Find the Last Digit in String Python

Find the Last Digit in String Python | In this tutorial, we’ll learn how to extract the last digit of a string in Python. If we have numbers in the given string, then we need to discover the last digit in the string. Such as “Know program, Happy new year 2025” and then print the last digit “5” from 2025.

We’ll write a Python program that uses regular expressions, set [0-9] and the characters ‘.’, ‘+’, ‘()’, ‘[ ]’, ‘^’, ‘*’, ‘$’ to get the last digit of a given integer. We can find the last digit in the given string by making use of the set [0-9] which will return values between the numbers 0-9.

While searching, the characters have their own use too, for instance, the  ‘.’ and ‘+’ mean any character, and one or more characters respectively. The [ ] represents a group and the ( ) makes our work easier by grouping things. Similarly, character ‘^’ is used to check the start, ‘*’ is for zero occurrences, and ‘$’ to check the end.

Program to Find the Last Digit in String Python

Now let us see some examples to find the last digit in string Python. Consider a random string with some numbers in it, we will find the last digit in the string using “re” expressions.

# RegEx
import re

# define string
mystring = "Know2025Program"

# give condition
last_num = re.match('.+([0-9])[^0-9]*$', mystring)

# print result
print("The last digit in the string is", last_num.group(1))

Output:-

The last digit in the string is 5

Approach:

1. We start with importing the regex expression as “re“.
2. The re.match() function has been used to find a pattern inside the “mystring”. If the search is effective, the function returns a match object.
3. In the next step we define a condition to find the last digit in the string. Here:-

  • The + symbol corresponds to one or more instances of the patterns on the left.
  • [123456789] is like [1-9].
  • Any character which is not a number is represented by [^0-9].
  • The * symbol indicates that there are zero or more instances.
  • To see if a string terminates with a specific character, use $.

4. We then print the result using the group metacharacter.

Let’s see another example to find the last digit in string Python:-

Consider the string “123 Hello World, 123 Welcome!”. The expected output here is 3. The program to find the last digit in string python is as follows:-

# RegEx
import re

# define string
mystring = "123 Hello World, 123 Welcome!"

# give condition
last_num = re.match('.+([0-9])[^0-9]*$', mystring)

# print result
print("The last digit in the string is", last_num.group(1))

Output:-

The last digit in the string is 3

If a string doesn’t contain any digit then it will display a message saying, the string doesn’t contain any digit. Consider the following program to print the last digit in string python:-

Program to Find the Last Digit in String Python when String may contain digits or not.

import re
mystring = "Know Program"
last_num = re.match('.+([0-9])[^0-9]*$', mystring)

# print result
if last_num:
   print("The last digit in the string is", last_num.group(1))
else:
   print("The given string does not contain any digit.")

Output:-

The given string does not contain any digit.

Thus we saw a program to print our desired output when there are no digits in the string. The regex function makes searching and matching in python very easy. In this article, we learned how to find the last digit in string python. We hope you liked the concepts!

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *