Python program to extract numeric suffix from string
Last Updated :
02 May, 2023
Given a string of characters with digits embedded in it. The task is to write a Python program to extract all the numbers that are trailing, i.e at the suffix of the string.
Examples:
Input : test_str = “GFG04”
Output : 04
Explanation : 04 is suffix of string and number hence extracted.
Input : test_str = “GFG143”
Output : 143
Explanation : 143 is suffix of string and number hence extracted.
Method #1 : Using loop + isdigit() + slicing
In this, the string is reversed using slicing, and occurrence of 1st non-digit element is recorded using isdigit, and at last, list is sliced till the element and reversed again to get the suffix number.
Python3
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
rev_str = test_str[:: - 1 ]
temp_idx = 0
for idx, ele in enumerate (rev_str):
if not ele.isdigit():
temp_idx = idx
break
res = rev_str[:temp_idx][:: - 1 ]
print ( "Suffix number : " + str (res))
|
Output
The original string is : GFG04
Suffix number : 04
Method #2: Using regex
Appropriate regex can be employed to solve this problem. Provides a compact solution to the problem.
Python3
import re
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
res = re.search(r "(\d+)$" , test_str).group()
print ( "Suffix number : " + str (res))
|
Output
The original string is : GFG04
Suffix number : 04
The time and space complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Without isdigit() method
Python3
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
rev_str = test_str[:: - 1 ]
temp = 0
digits = "0123456789"
for i, ele in enumerate (rev_str):
if not ele in digits:
temp = i
break
res = rev_str[:temp][:: - 1 ]
print ( "Suffix number : " + str (res))
|
Output
The original string is : GFG04
Suffix number : 04
Method#4: Using list comprehension
Python3
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
res = ''.join([char for char in test_str[:: - 1 ] if char.isdigit()])[:: - 1 ]
print ( "Suffix number : " + str (res))
|
Output
The original string is : GFG04
Suffix number : 04
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using filter and join method.
Python3
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
res = ''.join( filter ( str .isdigit, test_str[:: - 1 ]))[:: - 1 ]
print ( "Suffix number : " + str (res))
|
Output
The original string is : GFG04
Suffix number : 04
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#6: Using rfind() and string slicing
Step by step Algorithm:
- Find the index of the first non-digit character in the string using a lambda function and filter.
- Find the last occurrence of this non-digit character using rfind() function.
- Extract the suffix number using string slicing from the index of the non-digit character to the end of the string.
- Print the suffix number.
Python3
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
temp_idx = test_str.rfind( next ( filter ( lambda x: not x.isdigit(), test_str)))
res = test_str[temp_idx + 1 :]
print ( "Suffix number : " + str (res))
|
Output
The original string is : GFG04
Suffix number : 04
Time Complexity: O(n), where n is the length of the string. The lambda function and filter takes O(n) time, while rfind() and string slicing takes constant time.
Space Complexity: O(1), as we are not using any extra space.
Method #7: Using rpartition() + isdigit()
- Initialize the string
- Use the rpartition() method to split the string into 3 parts – the part before the last digit, the last digit, and the part after the last digit.
- Check if the last part (the part after the last digit) is empty, which indicates that the last character was a digit.
- If the last part is not empty, there was no numeric suffix in the original string, so print an appropriate message.
- If the last part is empty, check if the middle part (the last digit) is a digit.
- If the middle part is not a digit, there was no numeric suffix in the original string, so print an appropriate message.
- If the middle part is a digit, print it as the numeric suffix.
Python3
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
res = test_str.rpartition( 'GFG' )[ 2 ]
if res.isdigit():
print ( "Suffix number : " + str (res))
else :
print ( "No suffix number found in string!" )
|
Output
The original string is : GFG04
Suffix number : 04
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n), as we are creating a new string to store the extracted suffix.
Method #8: Using numpy:
- Initialize the input string.
- Convert the string to a NumPy array of characters.
- Convert the array to a string using NumPy’s char module.
- Find the indices of all the numeric characters in the string using the where() method from NumPy.
- Extract the suffix numbers by joining the characters at the found indices.
- Print the extracted suffix numbers.
Python3
import numpy as np
test_str = "GFG04"
print ( "The original string is : " + str (test_str))
char_array = np.array( list (test_str))
char_string = np.char.mod( '%c' , char_array)
indices = np.where(np.char.isdigit(char_string))[ 0 ]
suffix_numbers = ''.join(char_array[indices])
print ( "Suffix number: " + suffix_numbers)
|
Output:
The original string is : GFG04
Suffix number: 04
Time complexity:
The list() function in Python has a time complexity of O(n), where n is the length of the string. This operation is done once in our code.
The np.array() function in NumPy also has a time complexity of O(n), where n is the length of the string. This operation is also done once in our code.
The np.char.mod() function in NumPy has a time complexity of O(n), where n is the length of the string. This operation is also done once in our code.
The np.where() function in NumPy has a time complexity of O(n), where n is the length of the string. This operation is done once in our code.
The join() function in Python has a time complexity of O(n), where n is the length of the extracted suffix numbers. This operation is done once in our code.
Auxiliary Space:
The space complexity of our code depends on the length of the string. The input string is stored once as a string variable and then converted to a NumPy array, which also takes up space in memory. The space complexity is O(n), where n is the length of the string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...