Remove the given Substring from the End of a String using Python

When working with strings in Python, you may need to remove a specific substring from the end of a string. Python provides several built-in methods to accomplish this task efficiently, from simple string methods to regular expressions.

Using endswith() Method

The endswith() method checks if a string ends with a specific substring, making it perfect for conditional removal ?

def remove_substring(string, substring):
    if string.endswith(substring):
        return string[:len(string)-len(substring)]
    else:
        return string

# Example 
text = "Hello Everyone, I am John, The Sailor!"
last_substring = ", The Sailor!"
result = remove_substring(text, last_substring)
print(result)
Hello Everyone, I am John

Using String Slicing

String slicing allows you to compare and remove substrings by checking the last characters of the string ?

def remove_substring(string, substring):
    if string[-len(substring):] == substring:
        return string[:-len(substring)]
    else:
        return string

# Example 
whole_string = "Hello Everyone, I am John, the Sailor!"
last_substring = ", the Sailor!"
final_string = remove_substring(whole_string, last_substring)
print(final_string)
Hello Everyone, I am John

Using re Module

The re.sub() function with regular expressions provides powerful pattern matching for substring removal ?

import re

def remove_substring(string, substring):
    pattern = re.escape(substring) + r'$'
    return re.sub(pattern, '', string)

# Example
whole_string = "Hello Everyone, I am John, the Sailor!"
last_substring = ", the Sailor!"
final_string = remove_substring(whole_string, last_substring)
print(final_string)
Hello Everyone, I am John

Using rfind() with Slicing

The rfind() method finds the last occurrence of a substring and verifies it's at the string's end ?

def remove_substring(string, substring):
    index = string.rfind(substring)
    if index != -1 and index + len(substring) == len(string):
        return string[:index]
    else:
        return string

# Example 
whole_string = "Hello Everyone, I am John, the Sailor!"
last_substring = ", the Sailor!"
final_string = remove_substring(whole_string, last_substring)
print(final_string)
Hello Everyone, I am John

Using removesuffix() (Python 3.9+)

Python 3.9 introduced removesuffix(), the most straightforward method for this task ?

# Example using removesuffix()
text = "Hello Everyone, I am John, the Sailor!"
suffix = ", the Sailor!"
result = text.removesuffix(suffix)
print(result)
Hello Everyone, I am John

Comparison

Method Python Version Performance Best For
removesuffix() 3.9+ Fastest Simple suffix removal
endswith() All versions Fast Clear, readable code
String slicing All versions Fast Direct character comparison
re.sub() All versions Slower Complex pattern matching

Conclusion

Use removesuffix() if you're using Python 3.9+, otherwise endswith() provides the clearest approach. Choose re.sub() only when you need complex pattern matching for substring removal.

Updated on: 2026-03-27T10:45:52+05:30

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements