Remove a Substring from the end of a String in Python

In this Python tutorial, we will learn how to remove a substring from the end of a string.

Table Of Contents

Remove substring from the end of a string using Slicing

First, we have to check if the string ends with the given substring using the if condition. If True, then we can select the characters in it from index 0 till the point where substring starts. Basically, if the substring length is N, then select characters in string from index 0 till -N. It will remove the substring from the end of a string.

Syntax:

if string1.endswith(sub_string):
    string1 = string1[ :-(len(sub_string))]

Here, string1 is the actual string and sub_string is the sub-string to be removed from the end of string. We selected the characters from index 0 till -len(sub_string). It will give an effect that we have deleted the substring from the end of a string.

Advertisements

Example:1

In this example, we will consider a string – “Welcome to thisPointer”. Then we will remove the sub-string – ‘thisPointer’ from the end of the string.

# Consider the string
string1="Welcome to thisPointer"

# Actual String
print("Original String: ",string1)

subStr = 'thisPointer'

# Remove substring - 'thisPointer' from the end of string
if string1.endswith(subStr):
    string1 = string1[ : -(len(subStr))]

# Final String
print("Final String: ",string1)

output:

Original String:  Welcome to thisPointer
Final String:  Welcome to 

So the substring – ‘thisPointer’ is removed from the end of the actual string.

Example:2

In this example, we will consider a string – “Welcome to thisPointer Python Language”. Then we will remove the substring – ‘to thisPointer Python Language’ from the end of string

# Consider the string
string1="Welcome to thisPointer Python Language"

# Actual String
print("Original String: ",string1)

subStr = 'to thisPointer Python Language'

# Remove substring - 'thisPointer' from the end of string
if string1.endswith(subStr):
    string1 = string1[ : -(len(subStr))]

# Final String
print("Final String: ",string1)

output:

Original String:  Welcome to thisPointer Python Language
Final String:  Welcome 

So the substring – ‘to thisPointer Python Language’ is removed from the end of the actual string.

Remove substring from the end of a string using re.sub()

First, we have to check if the string ends with a substring using the if condition. If True, then we can use the re.sub() method available in the re module of Python. This sub() function takes three parameters,

  • The first parameter is the regex pattern.
  • The second parameter is the empty string, that replaces all the sub_string matched by the pattern.
  • The third parameter is the actual string.

Basically, it removes all the occurrences of substrings that matches the given regex pattern. Now in our case, we will pass a regex pattern that will match the substring at the end of the string only.

Example:1

In this example, we will consider a string – “Welcome to thisPointer”. Then we will remove the sub-string – ‘thisPointer’ from the end of string.

import re

# Consider the string
string1="Welcome to thisPointer"

# Actual String
print("Original String: ",string1)

subString = 'thisPointer'
pattern = subString + "$"

# Remove substring - 'thisPointer' from the end of string
if string1.endswith(subString):
    string1 = re.sub(pattern, '', string1)

# Final String
print("Final String: ",string1)

Output:

Original String:  Welcome to thisPointer
Final String:  Welcome to 

So the substring – ‘thisPointer’ is removed from the end of the actual string.

Remove substring from the end of a string using replace()

To remove the substring from the end of the actual string. First, we have to check if the string ends with a substring using the if condition. If true, then we use replace() method. It replaces the substring with an empty string by taking two parameters.

Syntax:

if string1.endswith(sub_string):
    string1 = string1.replace(sub_string, '')

Here, string1 is the actual string and sub_string is the sub-string to be removed from the last.

Parameters:

  1. the First parameter is the sub_string to be removed.
  2. the second parameter is the empty string that replaces the sub_string.

Example 1:

In this example, we will consider a string – “Welcome to thisPointer”. Then we will remove the sub-string – ‘thisPointer’.

# Consider the string
string1="Welcome to thisPointer"

# Actual String
print("Original String: ",string1)

# Remove substring - 'thisPointer'
if string1.endswith('thisPointer'):
    string1 = string1.replace('thisPointer', '')

# Final String
print("Final String: ",string1)

Output:

Original String:  Welcome to thisPointer
Final String:  Welcome to 

So the substring – ‘thisPointer’ is removed from the end of the actual string.

But this approach has a drawback. It will remove all occurrences of the substring from the string, instead of only the last one. We should use this approach only when our substring has only one occurrence in the string and it is in the end only.

Summary

In this article, we saw how to remove a substring from the end of a string in Python. We discussed three methods. In all these three methods, we have to first check if the sub-string that has to be removed is present at the end of the actual string, then only remove it. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top