Find first number in string in Python

In this Python tutorial, we will understand how to find first number in string in Python. Moreover, we will try to illustrate this task using multiple methods and examples.

We can find first number in string in Python using the following 2 ways.

  • Using re.search()
  • Using isdigit() & enumerate()

So, next, let us discuss each method using examples in Python.

Method 1: Find first number in string in Python using re.search()

The re module in Python allows using the regular expression functionality to find any particular pattern. And there are multiple methods in re module for searching or finding matching patterns.

Once such method is re.search() in Python.

The re.search() method analyzes a given string and returns the first matching pattern. Additionally, it returns the result as a match object. However, if no pattern matches, it returns None.

So, we can use the re.search() method to find the find number or digit occurring in the string. But, there are 2 ways by which we can specify the pattern to find digits.

Let us look at both the examples

Example-1

In this example, we used the re.search() to search the first number occurring in the sample_text string.

# Importing the re module
import re

# Defining a string with numbers
sample_text = 'California21Texas34Alaska76'

# Finding first number
match = re.search(r'\d+', sample_text)

if match:
    # Printing number and its index
    print('The first number is:', match.group())

    print('Index of first number is:', match.start())
else:
    print('The given string does not have any number')

Here we used \d+ to specify the pattern where it will search for the first occurrence of digits. Also, to get the result, we used the group() method, and to get the index, we used the start() method.

READ:  Python Dictionary Search by value

The execution of the above Python program is given below.

find first number in string in Python
Finding first number in string in Python using re.search()

Example-2

Another way to define the first matching pattern for digits is by using the [0-9] string in the re.search() method. Here is an example.

# Importing the re module
import re

# Defining a string with numbers
sample_text = 'Texas98California13Alaska76'

# Finding first number
match = re.search(r'[0-9]+', sample_text)

if match:
    # Printing number and its index
    print('The first number is:', match.group())

    print('Index of first number is:', match.start())
else:
    print('The given string does not have any number')

Once we execute the above Python program, we will get the following result.

first number in string in Python using re
Finding first number in string in Python using re module

So, in this section, we have seen how to find first number in string in Python using the re.search() method.

Read: Python program to reverse a string

Method 2: Find first number in string in Python using isdigit() & enumerate()

In this section, we will learn how to find first number in string in Python using isdigit() and enumerate() functions.

The enumerate() function in Python takes a collection and returns enumerate objects. This allows for not only the value but also its index. While isdigit() is used to check if the character in a given string is numeric or not.

Let us see an example where we will use both things together.

# Defining a string with numbers
sample_text = 'Texas98California13Alaska76'

# Iterating over each charcher in the string
for index, char in enumerate(sample_text):
    # Checking if char is numeric
    if char.isdigit():
        # Printing number and its position
        print('The first number is:', char)
        print('Index of first number is:', index)
        break
  • In the above example, first, we defined a string that contains alphabets and numbers.
  • Then we used the enumerate() function to iterate over each character given in the string and also gets their index.
  • Next, we utilized the isdigit() method on each character to check if the character is a number or not.
  • In the end, we will return the first single numeric value and its index.
READ:  Isdigit method in String Python

Here is the final result of the above Python program.

first number in Python string
Get first number in Python string

Note: Please note that this method will return only single number and its index. For better result, we recommend you to use the first method.

Read: Python string formatting

Conclusion

So, in this Python tutorial, we understood how to get or find first number in string in Python. Here we discussed the following 2 methods to find first number from Python String.

  • Using re.search()
  • Using isdigit() & enumerate()

You may also like to read the following Python tutorials.