Check if a string starts with a Number in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Check if a string starts with a Number in Python #

To check if a string starts with a number:

  1. Access the first character in the string.
  2. Use the str.isdigit() method to check if the character is a number.
  3. The str.isdigit() method will return True if the string starts with a number.
main.py
my_str = '248bobby' if my_str and my_str[0].isdigit(): # 👇️ this runs print('The string starts with a number') else: print('The string does NOT start with a number') print('248bobby'[0].isdigit()) # 👉️ True print('bobby'[0].isdigit()) # 👉️ False print('_bobby'[0].isdigit()) # 👉️ False

We first make sure that the string is not empty before accessing the character at index 0.

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

main.py
print('123'.isdigit()) # 👉️ True print('1.23'.isdigit()) # 👉️ False (contains period) print('-123'.isdigit()) # 👉️ False (contains minus)

If the character at index 0 is a digit, then the string starts with a number.

If you try to access the character at index 0 of an empty string, you'd get an error.

You can either use the boolean and operator as we did in the first example or use string slicing to avoid the error.
main.py
my_str = '' if my_str[:1].isdigit(): print('The string starts with a number') else: # 👇️ this runs print('The string does NOT start with a number')

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

The slice selects the first character in the string if it exists, otherwise an empty string is returned.

Alternatively, you can use the re.match() method.

Check if a string starts with a Number using re.match() #

Use the re.match() method to check if a string starts with a number, e.g. re.match(r'\d', string). The re.match() method will return a match object if the string starts with a number, otherwise None is returned.

main.py
import re def starts_with_number(string): return bool(re.match(r'\d', string)) my_str = '249bobbyhadz' if starts_with_number(my_str): # 👇️ this runs print('The string starts with a number') else: print('The string does NOT start with a number') print(starts_with_number('249bobby')) # 👉️ True print(starts_with_number('__bobby')) # 👉️ False print(starts_with_number('')) # 👉️ False

The re.match method returns a match object if the provided regular expression is matched in the string.

The match() method returns None if the string doesn't match the regex pattern.

We used the bool() class to convert the match object or None value to a boolean.

The first argument we passed to the re.match() method is a regular expression.

main.py
import re def starts_with_number(string): return bool(re.match(r'\d', string))

The \d character matches the digits [0-9] (and many other digit characters).

The re.match() method only checks if the characters at the beginning of the string match the regular expression, so it does exactly what we need.

If you ever need help reading or writing a regular expression, consult the regular expression syntax subheading in the official docs.

The page contains a list of all of the special characters with many useful examples.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.