Last modified: Jan 19, 2023 By Alexander Williams

3 Methods to Check if String Starts With a Number in Python

The string is zero or more characters written inside quotes and, it can be a mixture of integers, letters, symbols.

However, in this tutorial, we will be discussing three methods to check if a string starts with a number.

Method #1: Check using isdigit()

isdigit() is a method that returns True if all string's characters are integer. Overwise, return False.

Syntax

string.isdigit()

How to use isdigit() to check if a string starts with a number

To check if a string starts with a number, first, we'll get the first character of the string using the index statement. Then, check if the character is a number using the isdigit() method.

Let's see an axample:


# String
my_string = "100 houses"

# Get the first character 
f_character = my_string[0]

# Check if the first character is a number
print(f_character.isdigit())

Output:

True

As is evident in the output, the first character is a number.

But, if the string is empty, does this method work?

The answer is in the example below:


# String
my_string = ""

# Check if my_string is a number
print(my_string[0].isdigit())

Output:

IndexError: string index out of range

as you can see, we got the IndexError: string index out of range issue.

We can solve the issue by checking if the string is not empty as the following example:


# String
my_string = ""

# Check my_string 
if my_string:
    print(my_string[0].isdigit())
    
else:
    print("String is empty")

Output:

String is empty or None

 

Or using strings Slicing:

 


# String
my_string = ""

# isdigit() with Slicing
print(my_string[:0].isdigit())

Output:

False

The slicing operation does not raise an error if the index is out of range.

Do something after checking

If you want to do something after checking if the string starts with a number, you need to use the if statement.


my_string = "100 houses"

if my_string[0].isdigit():
    # Do something
    print('True')
else:
    # Do something
    print('False')

Output:

True

Checking multiple items

Let's say we have a list of items and, we want to check if they start with a number.

In the following example, we'll check a list of items:


my_list = ['cat1', 'dog2', '3tiger', 'bird']

for li in my_list:
    # Checking if the element starts with a number
    if li[0].isdigit():
        print(f"{li}: starts with a number")
    else:
         print(f"{li}: doesn't starts with a number")


Output:

cat1: doesn't starts with a number
dog2: doesn't starts with a number
3tiger: starts with a number
bird: doesn't starts with a number

Method #2: Check using startswith()

The startswith() method returns True if the string starts with the specified value or False if not.

Here, we'll use startswith() to check if a string starts with a specific number.

Syntax


string.startswith("number")

Example

In the following example, we'll check if the string starts with number one.


my_string = "100 houses"

# Check if my_string starts with number 1
print(my_string.startswith('1'))

Output:

True

Check with multiple numbers:


my_string = "100 houses"

print(my_string.startswith(('1', '2', '3')))

Output:

True

startswith(('1', '2', '3')) means if the string starts with 1 or 2 or 3.

Method #3 Check using Regex

You can also use regular expressions for checking if the string starts with a number. But, this method should be your last option.

let's see how we can do that with regex.


import re

# String
my_string = "100 houses"

# Search if start with a number
reg = re.search('^\s*[0-9]',my_string)

# Print result
print(reg)

Output:

<re.Match object; span=(0, 1), match='1'>

As demonstrated, we've used the search() function to check the string.

The search() function searches the string for a match, and if there is no match, the None value will be returned.

Conclusion

In conclusion, we've learned three methods to check if a string starts with a number.
isdigit() will be your best method for checking if string strat with a number. And, startswith() for checking if string strat with a specific number.