
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Substring from End of String in Python
Python is a globally used programming language used for different purposes by developers. Python has various different applications such as Web Development, Data Science, Machine Learning and also to perform different processes with automation. All the different programmers using python have to deal with strings and substrings. So in this article we are going to learn how to remove the substring present at the end of a string.
Different Methods to Remove Substrings
Using Functions
We are going to use the endswith() function which help us to remove the substring present at the end of a string. To understand it in a clearer way we will take the following example:
Example
def remove_substring(string, substring): #Defining two different parameters if string.endswith(substring): return string[:len(string)-len(substring)] #If substring is present at the end of the string then the length of the substring is removed from the string else: return string #If there is no substring at the end, it will return with the same length # Example text = "Hello Everyone, I am John, The Sailor!" last_substring = ", The Sailor!" #Specifying the substring #Do not forget to enter the last exclamation mark, not entering the punctuations might lead to error Without_substring = remove_substring(text, last_substring) print(Without_substring)
Output
The output of the above code will be as follows:
Hello Everyone, I am John
Slicing The String
In this method we will slice the substring present at the end of the string. Python provides the feature to slice a text or string present in the code. We will define the substring in the program and accordingly it will be sliced. The code along with the example to remove the substring using this method is as follows:
Example
def remove_substring(string, substring): if string[-len(substring):] == substring: #The length of last characters of the string (length of substring) is compared with the substring and if they are same the substring is removed return string[:-len(substring)] else: return string #If the length of last characters(Substring) does not match with the length of last substring then the characters are not removed # 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)
Output
The output of the above code will be as follows:
Hello Everyone, I am John
re module
re module present inside the Python programming language is used to work with regular functions. We can use one such function of re module to remove the substring at the end of the string. The function we will use is re.sub() function. The code and example to remove the last substring of the string using re module functions is as follows:
Example
import re #Do not forget to import re module or it might lead to error while running the program def remove_substring(string, substring): pattern = re.escape(substring) + r'$' #re.escape is used to create a pattern to treat all symbols equally and it includes $ to work only on the substring on the end of the string return re.sub(pattern, '', string) #This replaces the last substring with an empty space # 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)
Output
The output of the above code will be as follows:
Hello Everyone, I am John
Slicing along with a function
The rfind() function will be used in this case which start finding the defined substring from the right side and then we can remove the substring with the help of slicing feature. You can understand it in a better way with the help of following example:
Example
def remove_substring(string, substring): index = string.rfind(substring) #rfind() is used to find the highest index of the substring in the string if index != -1 and index + len(substring) == len(string): # If a substring is found, it is removed by slicing the string return string[:index] else: return string #If no substring is found the original string is returned # 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)
Output
The output of the above code will be as follows:
Hello Everyone, I am John
re module with capturing group
This is a different method of using re module for removing substring present at the end of the string. A capturing group is used along with the regular expression module to remove the substring. The code and example to remove the substring with the help of capturing group is as follows:
Example
import re #Do not forget to import the re module or error might occur while running the code def remove_substring(string, substring): pattern = re.escape(substring) + r'(?=$)' # A function is created first and re.escape is used so that all the functions are created equally return re.sub(pattern, '', string) # With the help of re.sub() function, the substring will be replaced with empty place # 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)
Output
The output of the above code will be as follows:
Hello Everyone, I am John
Conclusion
The need to make changes in the string is very common amongst all the users spread worldwide but many?a?times this process of removing the string can consume a lot of time if the correct approach is not followed. So, this article describes many different methods mentioned above which can be used to remove substring from the end of a string using python. There might be other methods possible to remove the substring but the methods mentioned in this article are the shortest and simplest methods suggested which you can select based on your field of application.