Find the index of the first Digit in a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Find the index of the first Digit in a String in Python #

To find the index of the first digit in a string:

  1. Use a for loop to iterate over the string with enumerate().
  2. Use the str.isdigit() method to check if each character is a digit.
  3. Once the index is found, use the break statement to break out of the loop.
main.py
my_str = 'bobby123hadz' for index, char in enumerate(my_str): if char.isdigit(): print(index, char) # 👉️ 5, 1 break

We used the enumerate() function to get access to the index of the current iteration.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_str = 'bobby' for index, char in enumerate(my_str): print(char, index) # 👉️ b 0, o 1, b 2, b 3, y 4
On each iteration, we use the str.isdigit() method to check if the current character is a digit.

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('A'.isdigit()) # 👉️ False print('5'.isdigit()) # 👉️ True

Once we find a digit, we use the break statement to break out of the loop.

main.py
my_str = 'bobby123hadz' for index, char in enumerate(my_str): if char.isdigit(): print(index, char) # 👉️ 5, 1 break

The break statement breaks out of the innermost enclosing for or while loop.

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

Find the index of the first Digit in a String using re.search() #

Use the re.search() method to find the index of the first digit in a string, e.g. re.search(r'\d', my_str).start(). The re.search() method looks for the first location in the string where the provided regular expression produces a match.

main.py
import re my_str = 'bobby123hadz' match = re.search(r'\d', my_str) if match: # 👇️ Index of first digit: 5 print('Index of first digit:', match.start()) # 👇️ Digit found: 1 print('Digit found:', match.group(0)) else: print('The string does NOT contain any digits')

The re.search method looks for the first location in the string where the provided regular expression produces a match.

If the re.search() method finds a match, it will return a match object, otherwise None is returned.

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

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

You can use the start() method on the match object to get the index of the first digit in the string.

If you need to get the first digit, pass 0 to the match.group() method.

When the number 0 is passed to the match.group() method, it returns the entire match.

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.