Python | Extract suffix after K
Last Updated :
25 Apr, 2023
Sometimes, we might have a use case in which we need to find a suffix in a string. But sometimes, the requirement can be something dynamic like a specific input character than a number of elements for the decision of getting suffix. Let’s discuss certain ways in which we can find suffixes of the string after a certain character.
Method #1 : Using rsplit()
This method originally performs the task of splitting the string from the rear end rather than the conventional left-to-right fashion. This can though be limited to 1, for solving this particular problem.
Python3
test_str = "GeeksforGeeks"
spl_char = "r"
print ( "The original string is : " + str (test_str))
res = test_str.rsplit(spl_char, 1 )[ 1 ]
print ( "The suffix string is : " + str (res))
|
Output :
The original string is : GeeksforGeeks
The suffix string is : Geeks
Method #2: Using rpartition()
If we need to solve this particular problem, this inbuilt function is recommended to perform this particular task. This function performs the partition as required just once from the rear end.
Python3
test_str = "GeeksforGeeks"
spl_char = "r"
print ( "The original string is : " + str (test_str))
res = test_str.rpartition(spl_char)[ 2 ]
print ( "The suffix string is : " + str (res))
|
Output :
The original string is : GeeksforGeeks
The suffix string is : Geeks
Method #3: Using slicing
Python3
test_str = "GeeksforGeeks"
spl_char = "r"
print ( "The original string is : " + str (test_str))
res = test_str[test_str.index(spl_char) + 1 :]
print ( "The suffix string is : " + str (res))
|
Output
The original string is : GeeksforGeeks
The suffix string is : Geeks
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using find()
The find() method is used to find the index of the first occurrence of k in the string. If k is not found in the string, find() returns -1. If k is found in the string, we return the suffix after the k character using string slicing.
Python3
def extract_suffix(string, k):
index = string.find(k)
if index = = - 1 :
return ""
else :
suffix = string[index + 1 :]
return suffix
string = "GeeksforGeeks"
k = "r"
suffix = extract_suffix(string, k)
print ( "The original string is:" , string)
print ( "The character to find is:" , k)
print ( "The extracted suffix is:" , suffix)
|
Output
The original string is: GeeksforGeeks
The suffix string is: Geeks
Time complexity: O(n)
Auxiliary space: O(n)
Method #5: Using numpy:
Algorithm:
- Convert the input string into a numpy array using np.array.
- Convert the split character into a numpy array.
- Use np.where to get the index of the last occurrence of the split character in the input array.
- Extract the suffix substring by slicing the input array from the index obtained in step 3 + 1 till the end.
- Join the suffix substring using the join function.
Python3
import numpy as np
test_str = "GeeksforGeeks"
spl_char = "r"
test_arr = np.array( list (test_str))
split_arr = np.array( list (spl_char))
res = ''.join(test_arr[np.where(test_arr = = split_arr)[ 0 ][ - 1 ] + 1 :])
print ( "The original string is: " + test_str)
print ( "The split character is: " + spl_char)
print ( "The suffix string is: " + res)
|
Output:
The original string is: GeeksforGeeks
The split character is: r
The suffix string is: Geeks
Time complexity: O(n), where n is the length of the input string. This is because the code involves iterating through each character in the input string until it finds the last occurrence of the split character.
Auxiliary space:O(1), because the code uses a constant amount of extra space (i.e., a few variables to store the input string, split character, and suffix substring). The space usage does not grow with the size of the input string.
Method 6: use the regular expression module re
Python3
import re
test_str = "GeeksforGeeks"
spl_char = "r"
print ( "The original string is : " + str (test_str))
pattern = re. compile (spl_char + '(.*)' )
res = pattern.search(test_str).group( 1 )
print ( "The suffix string is : " + str (res))
|
Output
The original string is : GeeksforGeeks
The suffix string is : Geeks
Time complexity: O(n) (where n is the length of the input string)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...