Python – Remove after substring in String
Given a String, remove all characters after particular substring.
Input : test_str = ‘geeksforgeeks is best for geeks’, sub_str = “for”
Output : geeksforgeeks is best for
Explanation : everything removed after for.Input : test_str = ‘geeksforgeeks is best for geeks’, sub_str = “is”
Output : geeksforgeeks is
Explanation : everything removed after is.
Method #1 : Using index() + len() + slicing
In this, we first get the index of substring to perform removal after, add to that its length using len() and then slice off elements after that string using slicing.
Python3
# Python3 code to demonstrate working of # Remove after substring in String# Using index() + len() + slicing # initializing stringstest_str = 'geeksforgeeks is best for geeks' # printing original stringprint("The original string is : " + str(test_str)) # initializing sub string sub_str = "best" # slicing off after length computationres = test_str[:test_str.index(sub_str) + len(sub_str)] # printing result print("The string after removal : " + str(res)) |
The original string is : geeksforgeeks is best for geeks The string after removal : geeksforgeeks is best
Method #2 : Using regex() ( for stripping off after numeric occurrence)
This is solution to a slightly different problem in which the string removal is required after numeric occurrence. We employ match operation and it retains all before match is found.
Python3
# Python3 code to demonstrate working of # Remove after substring in String# Using regex() ( for stripping off after numeric occurrence)import re # initializing stringstest_str = 'geeksforgeeks is best 4 geeks' # printing original stringprint("The original string is : " + str(test_str)) # slicing after the numeric occurrenceres = re.match(r"(.*\d+)", test_str).group() # printing result print("The string after removal : " + str(res)) |
The original string is : geeksforgeeks is best 4 geeks The string after removal : geeksforgeeks is best 4



