Python | Remove the given substring from end of string
Last Updated :
13 Mar, 2023
Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python.
Remove the substring from the end of the string using Slicing
In this method, we are using string slicing to remove the substring from the end.
Python3
text = 'GeeksforGeeksWorld'
sub = "World"
# find len of suffix
le = len(sub)
# slice out from string
text = text[:-le]
print(text)
Output:
GeeksforGeeks
Remove the substring from the end of the string using the Naive Method
In this method, we are using the Python loop and append method to remove the substring from the end.
Python3
# Initialising string
ini_string = 'xbzefdgstb'
# initializing string
sstring = 'stb'
# printing initial string and substring
print ("initial_strings : ", ini_string, "\nsubstring : ", sstring)
# removing substring from end
# of string using Naive Method
if ini_string.endswith(sstring):
res = ini_string[:-(len(sstring))]
# printing result
print ("resultant string", res)
Outputinitial_strings : xbzefdgstb
substring : stb
resultant string xbzefdg
Remove the substring from the end of the string using sub() method
In this method, we are using string regex sub() to remove the substring from the end.
Python3
import re
# Initialising string
ini_string = 'xbzefdgstb'
# initializing string
sstring = 'stb'
# printing initial string and substring
print ("initial_strings : ", ini_string, "\nsubstring : ", sstring)
# removing substring from end
# of string using sub Method
if ini_string.endswith(sstring):
res = re.sub(sstring, '', ini_string)
# printing result
print ("resultant string", res)
Outputinitial_strings : xbzefdgstb
substring : stb
resultant string xbzefdg
Remove the substring from the end of the string using replace() method
In this method, we are using the string replace() function to remove the substring from the end.
Python3
# Initialising string
ini_string = 'xbzefdgstb'
# initializing string
sstring = 'stb'
# printing initial string and substring
print ("initial_strings : ", ini_string, "\nsubstring : ", sstring)
# removing substring from end
# of string using replace Method
if ini_string.endswith(sstring):
res = ini_string.replace(sstring, '')
# printing result
print ("resultant string", res)
Outputinitial_strings : xbzefdgstb
substring : stb
resultant string xbzefdg
Using rfind() and slicing
rfind() returns the index of the last occurrence of the substring in the string. Then we slice the string from the beginning up to the index of the substring.
Python3
text = 'GeeksforGeeksWorld'
sub = "World"
# find the index of the substring
index = text.rfind(sub)
# slice out from the string
text = text[:index]
print(text)
#This code is contributed by Edula Vinay Kumar Reddy
Time complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1)
Similar Reads
Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Python - Remove after substring in String Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Python - Get all substrings of given string A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element
3 min read
Remove URLs from string in Python A regular expression (regex) is a sequence of characters that defines a search pattern in text. To remove URLs from a string in Python, you can either use regular expressions (regex) or some external libraries like urllib.parse. The re-module in Python is used for working with regular expressions. I
3 min read
Python | Substring removal in String list While working with strings, one of the most used application is removing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the removing of a substring in list of string is performed. Letâs discuss certain ways in which
5 min read
Python | Get the string after occurrence of given substring The problem involves getting the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python.Using partition()To extract the portion of a string that occurs after a specific substring partition() method is an efficient and
3 min read