Python | Remove the given substring from end of string Last Updated : 13 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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: GeeksforGeeksRemove 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 xbzefdgRemove 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 xbzefdgRemove 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 xbzefdgUsing 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 OutputGeeksforGeeks Time complexity: O(n), where n is the length of the string.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Python | Remove the given substring from end of string garg_ak0109 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python 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 Python | Frequency of substring in given string Finding a substring in a string has been dealt with in many ways. But sometimes, we are just interested to know how many times a particular substring occurs in a string. Let's discuss certain ways in which this task is performed. Method #1: Using count() This is a quite straightforward method in whi 6 min read How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways 2 min read Python | Get the substring from given string using list slicing Given a string, write a Python program to get the substring from given string using list slicing. Letâs try to get this using different examples. What is substring? A substring is a portion of a string. Python offers a variety of techniques for producing substrings, as well as for determining the in 4 min read Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing 3 min read Like