Skip to content
Related Articles

Related Articles

Python – Slice from Last Occurrence of K

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 22 Apr, 2020

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

 

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!