Open In App

Python | Extract suffix after K

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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)

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




# Define the function extract_suffix that
# takes a string and a character as input
 
 
def extract_suffix(string, k):
 
    # Find the index of k in the string using the find method
    index = string.find(k)
    # If k is not found in the string, return an empty string
    if index == -1:
        return ""
    # If k is found in the string, extract the suffix after it
    else:
        # The suffix starts at the index after k, so add 1 to the index
        suffix = string[index+1:]
        # Return the suffix string
        return suffix
 
 
# Define the input string and character
string = "GeeksforGeeks"
k = "r"
 
# Call the extract_suffix function with the input string and character
suffix = extract_suffix(string, k)
 
# Print the original string, character to find, and extracted suffix
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:

  1. Convert the input string into a numpy array using np.array.
  2. Convert the split character into a numpy array.
  3. Use np.where to get the index of the last occurrence of the split character in the input array.
  4. Extract the suffix substring by slicing the input array from the index obtained in step 3 + 1 till the end.
  5. Join the suffix substring using the join function.

Python3




import numpy as np
 
test_str = "GeeksforGeeks"
spl_char = "r"
 
# Extract suffix after K using numpy method
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
 
# 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 using regex
pattern = re.compile(spl_char + '(.*)')
res = pattern.search(test_str).group(1)
 
# printing result
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

Similar Reads