In this tutorial, I will explain how to remove prefixes from strings in Python. As a Python developer based in the USA, in one of my projects for Chicago clients, I encountered a situation where I needed to remove prefixes from strings in Python. After researching different methods, I found several effective solutions that I will share with you in this article. I will share my findings and provide detailed examples.
Remove Prefixes from Strings in Python
Python provides several ways to remove prefixes from strings in Python.
Read How to Convert a String to a Timestamp in Python?
1. Use the removeprefix() Method
Starting from Python 3.9, a dedicated removeprefix() method is available for strings. It removes the specified prefix and returns the rest of the string. If the prefix is not found, it returns the original string unchanged.
Example:
name = "Mr. John Doe"
cleaned_name = name.removeprefix("Mr. ")
print(cleaned_name)
Output:
John Doe
I have executed the above example code and added the screenshot below.
In this example, "Mr. " is removed from the name string using the removeprefix() method, resulting in "John Doe".
Read How to Return a String in Python?
2. Use String Slicing
If you are using a Python version lower than 3.9, you can achieve the same result using string slicing. This method involves finding the length of the prefix and slicing the string from that index onwards.
Example:
name = "Dr. Emily Johnson"
prefix = "Dr. "
cleaned_name = name[len(prefix):]
print(cleaned_name)
Output:
Emily Johnson
I have executed the above example code and added the screenshot below.
Here, we calculate the length of the "Dr. " prefix using len() and slice the name string starting from that index to remove the prefix.
Read How to Use a Raw Strings in Python?
3. Use Conditional Slicing
Another approach is to check if the string starts with the prefix using the startswith() method and then slice the string accordingly. This method is useful when you are unsure if the prefix exists in the string.
Example:
name = "Mrs. Olivia Brown"
prefix = "Mrs. "
if name.startswith(prefix):
cleaned_name = name[len(prefix):]
else:
cleaned_name = name
print(cleaned_name)
Output:
Olivia Brown
I have executed the above example code and added the screenshot below.
In this example, we first check if the name starts with the "Mrs. " prefix using the startswith() method. If it does, we slice the string to remove the prefix. Otherwise, we keep the original string unchanged.
Read How to Sort a String in Python?
Remove Multiple Prefixes
In real-world scenarios, you might encounter strings with different prefixes. To handle such cases, you can create a list of prefixes and iterate over them to remove any matching prefix from the string.
Example:
name = "Mr. Michael Smith"
prefixes = ["Mr. ", "Mrs. ", "Ms. ", "Dr. "]
for prefix in prefixes:
if name.startswith(prefix):
cleaned_name = name[len(prefix):]
break
else:
cleaned_name = name
print(cleaned_name) # Output: "Michael Smith"
In this example, we have a list of common prefixes in the prefixes list. We iterate over each prefix and check if the name starts with it using the startswith() method. If a matching prefix is found, we remove it using slicing and break out of the loop. If no prefix is found, the original string is kept as is.
Read How to Extract a Substring Between Two Characters in Python?
Conclusion
In this tutorial, I have explained how to remove prefixes from strings in Python. I discussed the removeprefix() method, string slicing, conditional slicing, and how to remove multiple prefixes.
You may also like to read:
- How to Fix Unterminated String Literals in Python?
- How to Use Python Triple Quotes with F-Strings for Multiline Strings?
- How to Do Case-Insensitive String Comparisons in Python?
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn