Python – Remove after substring in String
Last Updated :
11 Apr, 2023
Given a String, remove all characters after a 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
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
sub_str = "best"
res = test_str[:test_str.index(sub_str) + len (sub_str)]
print ( "The string after removal : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
The string after removal : geeksforgeeks is best
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of string.
Method #2: Using regex() ( for stripping off after numeric occurrence)
This is a solution to a slightly different problem in which string removal is required after the numeric occurrence. We employ match operation and it retains all before the match is found.
Python3
import re
test_str = 'geeksforgeeks is best 4 geeks'
print ( "The original string is : " + str (test_str))
res = re.match(r "(.*\d+)" , test_str).group()
print ( "The string after removal : " + str (res))
|
Output
The original string is : geeksforgeeks is best 4 geeks
The string after removal : geeksforgeeks is best 4
Method #3: Using split() method
Algorithm
- Initialize an empty list res to hold the resulting substrings.
- Initialize a variable start to 0, which represents the starting index of the current substring.
- While K is in test_str:
a. Find the index of the first occurrence of K in test_str, starting from the index start.
b. Append the substring from start to the index found in step 3a (excluding K) to the list res.
c. Update start to be the index found in step 3a + the length of K.
d. Update test_str to be the substring of test_str starting from the index found in step 3a + the length of K.
- Append the remaining substring to the list res.
- Return the list res
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
sub_str = "best"
re = test_str.split(sub_str)
res = re[ 0 ] + sub_str
print ( "The string after removal : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
The string after removal : geeksforgeeks is best
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4:Using str.find() and str.slice()
Here is the step-by-step algorithm
- Initialize a string ‘test_str’ with the value “geeksforgeeks is best for geeks”.
- Print the original string.
- Initialize a substring ‘sub_str’ with the value “best”.
- Check if the substring ‘sub_str’ is present in the string ‘test_str’ using the ‘in’ operator.
- If the substring is present, find its index in the string using the ‘find()’ method.
- Slice the string ‘test_str’ up to the index of the substring plus the length of the substring to remove the portion of the string after the substring.
- Store the resulting string in the variable ‘res’.
- Print the final string after removing the portion of the string.
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
sub_str = "best"
if sub_str in test_str:
res = test_str[:test_str.find(sub_str) + len (sub_str)]
print ( "The string after removal : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
The string after removal : geeksforgeeks is best
The time complexity of the algorithm is O(N), where N is the length of the string ‘test_str’. This is because the ‘find()’ method takes O(N) time in the worst case to find the substring in the string.
The Auxiliary space of the algorithm is O(N), where N is the length of the string ‘test_str’. This is because we are storing the original string ‘test_str’ and the resulting string ‘res’ in memory.
Method#5: reduce() function from the functools module:
Algorithm:
- Initialize the input string test_str as ‘geeksforgeeks is best for geeks’.
- Initialize the substring sub_str as ‘best’.
- Define a function remove_substring(s, sub) that takes two string arguments s and sub and returns a modified string.
- In the function remove_substring, find the index of the substring sub in the string s using the find() method.
- If the index is not -1, slice the string s up to the index of the substring plus the length of the substring using slicing, and return the modified string.
- Otherwise, return the original string s.
- Use the reduce() function from the functools module to apply the function remove_substring to the input string test_str and the substring sub_str.
- Print the original string test_str and the modified string res.
Python3
from functools import reduce
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
sub_str = "best"
def remove_substring(s, sub):
idx = s.find(sub)
if idx ! = - 1 :
return s[:idx + len (sub)]
return s
res = reduce ( lambda s, sub: remove_substring(s, sub), [sub_str], test_str)
print ( "The string after removal : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
The string after removal : geeksforgeeks is best
The time complexity: O(nm), where n is the length of the input string test_str and m is the length of the substring sub_str. This is because the find() method used in the remove_substring() function has a worst-case time complexity of O(n), and it is executed once for each substring in the reduce operation.
Auxiliary space:O(n), as the input string test_str is stored in memory along with the output string res. The remove_substring() function and the lambda function used in the reduce operation do not create any significant additional memory usage.
Method#6: Using replace method:
Algorithm :
- Initialize the string test_str with a given value.
- Print the original string test_str.
- Initialize the substring sub_str with a given value.
- Find the index of the sub_str in the test_str.
- Remove the portion of the string after the sub_str.
- Assign the new string to the variable res.
- Print the resulting string.
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
sub_str = "best"
res = test_str.replace(test_str[test_str.find(sub_str) + len (sub_str):], '')
print ( "The string after removal : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
The string after removal : geeksforgeeks is best
Time complexity: O(n), where n is the length of the string. In this case, finding the index of the sub_str and the length of the sub_str will take O(n) time, and so will the replace function. Therefore, the time complexity of the code is O(n).
Auxiliary Space: O(n), as we are using variables to store the string values.
Please Login to comment...