Check If String starts with a Number in Python

This article will discuss different ways to check if a string starts with a number in Python.

Table of Contents

Check if a String starts with a Number using isdigit()

In Python, the string class provides a member function isdigit(). Which returns True if the string contains digits only; otherwise it returns False. We can select the string’s first character as a sub-string and check if it is a digit. This way, we can confirm if a string starts with a number or not. Let’s see some examples,

Example 1:

sample_str = "7th version"

# Check if String starts with a Number
if sample_str[0].isdigit():
    print("The String '%s' starts with a number" % (sample_str))
else:
    print("The String '%s' does not starts with a number" % (sample_str))

Output:

Advertisements
The String '7th version' starts with a number

Here we used a string that started with a numeric character.

Example 2:

sample_str = "Sample of 11 Strings"

# Check if String starts with a Number
if sample_str[0].isdigit():
    print("The String '%s' starts with a number" % (sample_str))
else:
    print("The String '%s' does not starts with a number" % (sample_str))

Output:

The String 'Sample of 11 Strings' does not starts with a number

Here we used a string with a numeric character, but the string does not start with a number.

Check if a String starts with a Number using Regex

In Python, the regex module provides a function search(). It accepts a regex pattern and string as arguments. It looks for given regex pattern in the given string. If a match to the pattern is found, it returns a Match object; otherwise, it returns None if no match is found. We can check if a string starts with a number using the regex pattern “^\d”.

The caret ^ has a special meaning in a regex. It is called the “anchor”. The caret “^” matches at the beginning of the text. By using “\d” after the caret “^” i.e. “^\d”, we are specifying that string must start with a digit.

Let’s see some examples,

Example 1:

import re

sample_str = "7th version"

# Check if String starts with a Number
if re.search("^\d", sample_str) is not None:
    print("The String '%s' starts with a number" % (sample_str))
else:
    print("The String '%s' does not starts with a number" % (sample_str))

Output:

The String '7th version' starts with a number

Here we used a string that started with a numeric character.

Example 2:

import re

sample_str = "The 7th version"

# Check if String starts with a Number
if re.search("^\d", sample_str) is not None:
    print("The String '%s' starts with a number" % (sample_str))
else:
    print("The String '%s' does not starts with a number" % (sample_str))

Output:

The String 'The 7th version' does not starts with a number

It was a negative test. Here we used a string with a numeric character, but the string does not start with a number.

Example 3:

import re

sample_str = ""

if re.search("^\d", sample_str) is not None:
    print("The String '%s' starts with a number" % (sample_str))
else:
    print("The String '%s' does not starts with a number" % (sample_str))

Output:

The String '' does not starts with a number

It was a negative test. Here we used an empty string.

Summary:

We learned about two different ways to check if a string starts with a number or not in Python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top