In this tutorial, we will look at how to remove a substring from a string in Python with the help of some examples.
How to remove a substring from a string?

You can use the string replace()
function to remove a substring from a string in Python. Use the replace()
function to replace the substring with an empty string. Let’s look at an example –
# create a string s = "Wingardium...Leviosa..." # remove substring s.replace("...", '')
Output:
WingardiumLeviosa
Here, we removed the substring “…” from the string “Wingardium…Leviosa…”.
Note the that string replace()
function will replace every occurrence of the substring in the string.
Remove substring from the beginning of a string
If you specifically want to remove a substring only from the beginning of a string, slice the string starting from the length of the substring.
# create a string s = "Capt. Rogers and Capt. Bakshi" # store the substring to be removed from the beginning sub = "Capt." # remove substring s[len(sub):]
Output:
' Rogers and Capt. Bakshi'
The substring is removed from the front. Since we only want to remove the substring from the beginning, we use slicing to only keep the part of the string that comes after the substring.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Also, note that this method will not work if the substring is not present at the beginning of the string. This is because we are slicing the string irrespective of whether the substring is present or not in the string.
Remove substring from the end of a string
Similarly, if you want to remove a substring only from the end of a string, slice the string from its beginning to the starting index of the substring. Using a negative index can be helpful here.
# create a string s = "one mississippi two mississippi" # store the substring to be removed from the end sub = "mississippi" # remove substring s[:-len(sub)]
Output:
'one mississippi two '
The resulting string doesn’t have the substring in the end.
Again, note that this method will not work if the substring is not present at the end of the string. This is because we are slicing the string irrespective of whether the substring is present or not in the string.
You might also be interested in –
- Remove First Character From String in Python
- Remove Last Character From String in Python
- Python – Remove Non Alphanumeric Characters from String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.