Python | Check Numeric Suffix in String
Last Updated :
15 May, 2023
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
import re
test_str = "Geeks4321"
print ( "The original string is : " + str (test_str))
res = re.search(r '\d+$' , test_str)
print ( "Does string contain suffix number ? : " + str ( bool (res)))
|
Output
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
test_str = "Geeks4321"
print ( "The original string is : " + str (test_str))
res = test_str[ - 1 ].isdigit()
print ( "Does string contain suffix number ? : " + str (res))
|
Output
The original string is : Geeks4321
Does string contain suffix number ? : True
The time complexity of this program is O(1) since it only checks if the last character of the input string is a digit using the isdigit() method.
The auxiliary space required is also O(1) since the program does not use any additional data structures and only stores the result of the isdigit() method in a boolean variable res.
Method #3: Without using inbuilt function
Python3
test_str = "Geeks4321"
print ( "The original string is : " + str (test_str))
res = False
numbers = "0123456789"
if test_str[ - 1 ] in numbers:
res = True
print ( "Does string contain suffix number ? : " + str (res))
|
Output
The original string is : Geeks4321
Does string contain suffix number ? : True
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 4: Using the try and except block
This method using try and except block, try to convert the last character of string to int. If it throws error i.e. not a number, returns False, otherwise returns true.
Python3
test_str = "Geeks4321"
print ( "The original string is : " + test_str)
res = False
try :
int (test_str[ - 1 ])
res = True
except ValueError:
res = False
print ( "Does string contain suffix number ? : " + str (res))
|
Output
The original string is : Geeks4321
Does string contain suffix number ? : True
Time complexity: O(1)
Auxiliary Space: O(1)
Method 5: Using str.isdecimal() method
Python3
test_str = "Geeks4321"
print ( "The original string is : " + str (test_str))
res = False
if test_str[ - 1 ].isdecimal():
res = True
print ( "Does string contain suffix number ? : " + str (res))
|
Output
The original string is : Geeks4321
Does string contain suffix number ? : True
Time complexity: O(1)
Auxiliary Space: O(1)
Method 6: Using reduce():
Step-by-step approach:
- Import the reduce() function from the functools module.
- Initialize the string to be tested.
- Use the reduce() function to iterate through the characters of the string from the end and check if the last character is a number.
- If the last character is numeric, the lambda function returns True and the next character is checked.
- If all characters are numeric, the final value returned by reduce() is True.
- Print the result.
Python3
from functools import reduce
test_str = "Geeks4321"
print ( "The original string is : " + str (test_str))
res = reduce ( lambda x, y: y.isnumeric(), test_str[ - 1 ], False )
print ( "Does string contain suffix number? : " + str (res))
|
Output
The original string is : Geeks4321
Does string contain suffix number? : True
Time Complexity:
The time complexity of the reduce() function is O(n), where n is the length of the input string. Since the lambda function is called once for each character in the string, the overall time complexity of the algorithm is also O(n).
Space Complexity:
The space complexity of the algorithm is O(1), since only a few variables are used to store the input string and the result, and their space requirements do not depend on the length of the string. The lambda function used in the reduce() function does not create any additional data structures that would increase the space complexity. Therefore, the overall space complexity of the algorithm is constant.
Method #7: Using a loop to iterate over the characters
- Initialize the string to be checked for a numeric suffix.
- Initialize a boolean variable res to False.
- Use a loop to iterate over the characters in the string from right to left.
- For each character, check if it is a digit using the isdigit() method.
- If the character is a digit, set res to True and break out of the loop.
- If the character is not a digit, set res to False and continue iterating over the rest of the string.
- Print the result.
Python3
test_str = "Geeks4321"
print ( "The original string is : " + test_str)
res = False
for c in reversed (test_str):
if c.isdigit():
res = True
break
else :
res = False
print ( "Does string contain suffix number? : " + str (res))
|
Output
The original string is : Geeks4321
Does string contain suffix number? : True
Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(1), as the space used is constant regardless of the input size.
Share your thoughts in the comments
Please Login to comment...