3 Ways to Find Last Number in String in Python

In this Python tutorial, we will understand how to find last number in string in Python.

We can find the last number in string in Python using regular expression. And in Python, we can use the functionality of regular expression using re module. Additionally, using re module also there following 3 ways to get last number in string in Python.

  • Using re.findall()
  • Using re.search()
  • Using re.match()

Next, let us discuss each of these methods using examples.

Method 1: Find Last Number in String in Python Using re.findall()

In this section, we will understand how to find last number in string in Python using the re.findall() function.

The re.findall() is a built-in function that comes under the re module. As the name suggests, the role of this function is to find and return all the matches for a given pattern.

So, by using the re.findall() function, we can find the list of all digits occurring in the string. And then, we can fetch the last number from the list using the -1 index.

Here is an example of this implementation in Python.

# Importing the re module
import re

# Defining a string with numbers
sample_text = 'Texas98California13Alaska76'

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

if match:
    # Printing number
    last_num = match[-1]
    print('The last number is:', last_num)

else:
    print('The given string does not have any number')
  • In the above example, first, we utilized the re.findall() function to fetch all the digits from the sample_text.
  • After this, we used the [-1] indexing to fetch only the last digit from the list.
  • In the end, we printed the last number as the output of the Python program.

Here is the result of the above python program.

Find last number in string in Python
Finding last number in string in Python

So, in this section, we have seen how to find last number in string using the re.findall() function in Python.

Read: Python compare strings

Method 2: Find Last Number in String in Python Using re.search()

In this section, we will understand how to find last number in string in Python using the re.search() function.

  • The re.search() function is also the part of re module in Python. The main role of using this function is to search for a given pattern.
  • And this function returns the match object as the result. However, if the given pattern does not matches, it returns None.

Here is an example of using the re.search() function to get the last number from the string.

# Importing the re module
import re

# Defining a string with numbers
sample_text = 'Texas98California13Alaska76'

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

if match:
    # Printing last number
    print('The last number is:', match.group()[::-1])

else:
    print('The given string does not have any number')
  • In the above example, first, we utilized the re.search() function to fetch the match pattern for all digits.
  • After this, we used the indexing on the match object to get the last number from the string.

The execution of the above Python program is shown below.

Find last number in Python string
Finding the last number in Python string

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

Read: Python split string by space

Method 3: Find Last Number in String in Python Using re.match()

In this section, we will understand how to find last number in string in Python using the re.match() function.

  • The re.match() function in Python comes under the re module which is a common module utilized for the regular expression.
  • This function is quite similar to the re.search() function. However, the re.match() only searches for patterns at the start of the string only.
  • But, if it does not find any match, it returns None instead.

Let us look at an example of using this function in Python.

# Importing the re module
import re

# Defining a string with numbers
sample_text = 'Texas98California13Alaska76'

# Finding last number
match = re.match('.+([0-9])[^0-9]*$', sample_text)

if match:
    # Printing number
    print('The last number is:', match.group(1))

else:
    print('The given string does not have any number')

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

Find last number in string in Python using re module
Finding the last number in string in Python using re.match()

Note: Please note that in this method, we will only get single digit as a result.

So, in this section, we have seen how to find the last number in string using the re.match() function in Python.

Conclusion

So, at the end of this Python tutorial, we understood how to get or find last number in string in Python using the following 3 methods.

  • Using re.findall()
  • Using re.search()
  • Using re.match()

You may also like to read the following Python tutorials.