Skip to content
Related Articles

Related Articles

Python | Extract suffix after K

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Sep, 2022
View Discussion
Improve Article
Save Article

Sometimes, we might have a use case in which we need to find a suffix in string. But sometimes, the requirement can be something dynamic like a specific input character than number of elements for the decision of getting suffix. Let’s discuss certain ways in which we can find suffix of 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




# Python3 code to demonstrate working of
# Extract suffix after K
# Using rsplit()
 
# initializing string
test_str = "GeeksforGeeks"
 
# initializing split character
spl_char = "r"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using rsplit()
# Extract suffix after K
res = test_str.rsplit(spl_char, 1)[1]
 
# printing result
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




# Python3 code to demonstrate working of
# Extract suffix after K
# Using rpartition()
 
# initializing string
test_str = "GeeksforGeeks"
 
# initializing split character
spl_char = "r"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using rpartition()
# Extract suffix after K
res = test_str.rpartition(spl_char)[2]
 
# printing result
print("The suffix string is : " + str(res))

Output : 

The original string is : GeeksforGeeks
The suffix string is : Geeks

Method #3 : Using slicing 

Python3




# Python3 code to demonstrate working of
# Extract suffix after K
 
# initializing string
test_str = "GeeksforGeeks"
 
# initializing split character
spl_char = "r"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Extract suffix after K
res=test_str[test_str.index(spl_char)+1:]
 
# printing result
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)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!