How do I remove a substring from the end of a string in Python?



In this article, we are going to find out how to remove a substring from the end of a string in Python.

The first approach is by using slicing approach. In this method, we will check if the string is ending with the given substring or not, if it is ending with the given substring then we will slice the string, removing the substring.

The ability to access portions of sequences like strings, tuples, and lists in Python is known as slicing. Additionally, you can use them to add, remove, or edit the elements of mutable sequences like lists. Slices can also be used with external objects like Pandas series, data frames, and NumPy arrays.

Example

In the example given below, we are taking a string and a substring as input and we are removing that substring from the end of the string using slicing 

def remove_substr(str,sub):
   if str.endswith(sub):
      return str[:-len(sub)]
   return str
   
str1 = "Welcome to Tutorialspoint"
print("The given string is ")
print(str1)

substr = "point"
print("The given substring is")
print(substr)

print("Removing the substring from the string")
print(remove_substr(str1,substr))

Output

The output of the above example is as follows −

The given string is
Welcome to Tutorialspoint
The given substring is
point
Removing the substring from the string
Welcome to Tutorials

Using sub() method of Regular Expressions

The second approach is by using the sub() method of Regular expressions. This method takes 3 parameters, the substring that you want to substitute, the substring with which you are going to substitute, and the main string. So, we are going to send the end as first parameter, empty string as second parameter ,and main string as third parameter.

Example

In the example given below, we are taking a string and substring as input and we are removing the substring at the end using sub() method 

import re
def remove_substr(str,sub):
   if str.endswith(sub):
      res = re.sub(sub, '', str)
      return res
   return str

str1 = "Welcome to Tutorialspoint"
print("The given string is ")
print(str1)

substr = "point"
print("The given substring is")
print(substr)

print("Removing the substring from the string")
print(remove_substr(str1,substr))

Output

The output of the above example is as shown below −

The given string is
Welcome to Tutorialspoint
The given substring is
point
Removing the substring from the string
Welcome to Tutorials

Using replace() method

The third approach is by using the replace() method. This method takes 2 parameters, the substring that is to be replaced and the substring with which we are going to replace. So here, the first parameter to be sent is the ending and the next parameter will be empty space.

Example

In the example given below, we are taking a string and a substring as input and we are removing the substring from the ending of the string using replace method 

def remove_substr(str,sub):
   if str.endswith(sub):
      res = str1.replace(sub, '')
      return res
   return str
   
str1 = "Welcome to Tutorialspoint"
print("The given string is ")
print(str1)

substr = "point"
print("The given substring is")
print(substr)

print("Removing the substring from the string")
print(remove_substr(str1,substr))

Output

The output of the above example is given below −

The given string is
Welcome to Tutorialspoint
The given substring is
point
Removing the substring from the string
Welcome to Tutorials

Advertisements