Python - Slice from Last Occurrence of K
Last Updated :
23 Apr, 2023
Sometimes, while working with Python Strings, we can have a problem in which we need to perform the task of performing characters stripping on the last occurrence of element. This can have applications in which data is involved. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + string slicing The combination of above methods can be used to solve this problem. In this, we search for the last occurrence using loop and save index to later slice.
Python3
# Python3 code to demonstrate working of
# Slice from Last Occurrence of K
# Using string slicing and loop
# initializing string
test_str = 'geeksforgeeks-is-best-for-geeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing K
K = "-"
# Slice from Last Occurrence of K
# Using string slicing and loop
idx = None
for i in range(len(test_str)):
if K == test_str[i]:
idx = i
res = test_str[:idx]
# printing result
print("Sliced String is : " + str(res))
Output : The original string is : geeksforgeeks-is-best-for-geeks
Sliced String is : geeksforgeeks-is-best-for
Time Complexity: O(n) where n is the length of the input string.
Auxiliary Space: O(n)
Method #2: Using rfind() + string slicing The combination of above methods can be used to solve this problem. In this, we extract the last occurrence using rfind() and rest slicing as above method.
Python3
# Python3 code to demonstrate working of
# Slice from Last Occurrence of K
# Using rfind() + string slicing
# initializing string
test_str = 'geeksforgeeks-is-best-for-geeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing K
K = "-"
# Slice from Last Occurrence of K
# Using rfind() + string slicing
idx = test_str.rfind(K)
res = test_str[:idx]
# printing result
print("Sliced String is : " + str(res))
Output : The original string is : geeksforgeeks-is-best-for-geeks
Sliced String is : geeksforgeeks-is-best-for
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using max() method
Python3
# Python3 code to demonstrate working of
# Slice from Last Occurrence of K
# Using string slicing and loop
# initializing string
test_str = 'geeksforgeeks-is-best-for-geeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing K
K = "-"
x=[]
res=[]
for i in range(0,len(test_str)):
if(test_str[i]==K):
x.append(i)
res=test_str[:max(x)]
# printing result
print("Sliced String is : " + str(res))
OutputThe original string is : geeksforgeeks-is-best-for-geeks
Sliced String is : geeksforgeeks-is-best-for
Time complexity : O(n), where n is length of test_str
Auxiliary space : O(n), where n is length of res string.
Method#4: Using split() and join()
this approach involves splitting the input string using the separator K, joining all the parts except the last one using the same separator K, and returning the joined string.
Algorithm
1. Split the string using K as the separator.
2. Join all the parts except the last one using K as the separator.
3. Return the joined string.
Python3
def slice_last_occurrence(test_str, K):
parts = test_str.split(K)
sliced_str = K.join(parts[:-1])
return sliced_str
test_str = 'geeksforgeeks-is-best-for-geeks'
K = "-"
print(slice_last_occurrence(test_str, K))
Outputgeeksforgeeks-is-best-for
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), where n is length of string
Similar Reads
Split on last occurrence of delimiter-Python The goal here is to split a string into two parts based on the last occurrence of a specific delimiter, such as a comma or space. For example, given the string "gfg, is, good, better, and best", we want to split it into two parts, everything before the last comma-space and everything after it. The r
3 min read
Python - Mid occurrence of K in string Given a String, the task is to write a Python program to extract the mid occurrence of a character. Input : test_str = "geeksforgeeks is best for all geeks", K = 'e' Output : 10 Explanation : 7 occurrences of e. The 4th occurrence [mid] is at 10th index. Input : test_str = "geeksforgeeks is best for
6 min read
Last Occurrence of Some Element in a List - Python We are given a list we need to find last occurrence of some elements in list. For example, w = ["apple", "banana", "orange", "apple", "grape"] we need to find last occurrence of string 'apple' so output will be 3 in this case.Using rindex()rindex() method in Python returns the index of the last occu
3 min read
Split string on Kth Occurrence of Character - Python The task is to write Python program to split a given string into two parts at the KáµÊ° occurrence of a specified character. If the character occurs fewer than K times return the entire string as the first part and an empty string as the second part. For example, in the string "a,b,c,d,e,f", splitting
3 min read
Python | Replace characters after K occurrences Sometimes, while working with Python strings, we can have a problem in which we need to perform replace of characters after certain repetitions of characters. This can have applications in many domains including day-day and competitive programming. Method #1: Using loop + string slicing This is brut
5 min read