Python | Check Numeric Suffix in String
Sometimes, while programming, we can have such a problem in which we need to check if any string is ending with a number i.e it has a numeric suffix. This problem can occur in Web Development domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using regex
This problem can be solved using regex. The search and group function can perform the task of searching the suffix string and printing the number, if it is required.
# Python3 code to demonstrate working of # Check Numeric Suffix in String # Using regex import re # initializing string test_str = "Geeks4321" # printing original string print("The original string is : " + str(test_str)) # Using regex # Check Numeric Suffix in String res = re.search(r'\d+$', test_str) # printing result print("Does string contain suffix number ? : " + str(bool(res))) |
The original string is : Geeks4321 Does string contain suffix number ? : True
Method #2 : Using isdigit()
The isdigit function can be used to perform this particular task using the fact that if a number at the end, means its very last character is going to be a number, so by just checking the last character, we can prove that a string ends with a number.
# Python3 code to demonstrate working of # Check Numeric Suffix in String # Using isdigit() # initializing string test_str = "Geeks4321" # printing original string print("The original string is : " + str(test_str)) # Using isdigit() # Check Numeric Suffix in String res = test_str[-1].isdigit() # printing result print("Does string contain suffix number ? : " + str(res)) |
The original string is : Geeks4321 Does string contain suffix number ? : True
Recommended Posts:
- Python | Check if given string is numeric or not
- Python | Check if suffix matches with any string in given list
- Python | Get numeric prefix of given string
- Python | Get the numeric prefix of given string
- Python | Ways to remove numeric digits from given string
- Python Regex to extract maximum numeric value from a string
- Python | Convert numeric String to integers in mixed List
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- Python | Check for URL in a String
- Python | Check if string repeats itself
- Python | Check for float string
- Python | Check whether string contains only numbers or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

