Introduction
There are multiple ways to remove whitespace and other characters from a string in Python. The most commonly known methods are strip()
, lstrip()
, and rstrip()
. Since Python version 3.9, two highly anticipated methods were introduced to remove the prefix or suffix of a string: removeprefix()
and removesuffix()
.
In this guide, we'll quickly go over how to use these methods, and why they are handy. We'll also show how to remove the prefix and suffix for Python versions lower than 3.9 with the help of the startswith()
and endswith()
methods.
If you'd like to learn more about
strip()
,lstrip()
andrstrip()
, read our Guide to Python's strip() Method and its Variants.
Why Not Use the strip() Methods?
You might wonder what's wrong with using lstrip()
and rstrip()
methods to trim the prefixes and suffixes? Nothing is wrong with them, however, there's a possibility of removing the entire text with these methods instead of just removing the first or last occurrence alone.
For instance, let's attempt removing the prefix in a string with a common pattern:
line = "xyyyxyxyxy"
print(line.lstrip("xy"))
This code strips the string pairing "xy" in any order, starting from the left. Since our input string only contains "xy" pairs, the entire text gets stripped away:
$ python strip_test.py
$
The strip()
methods won't cut it if you only need to remove the first or last n characters. This requirement was common in the Python community, and as an answer - the removeprefix()
and removesuffix()
methods were added.
Depending on the Python version (< 3.9 or > 3.9), there are two ways by which one can remove prefixes or suffixes from a string.
Remove Prefix/Suffix in Python Versions >= 3.9
For Python versions 3.9 and above, the removeprefix()
and removesuffix()
methods come as methods built-in to the namespace, used to remove the prefix and suffix from strings.
Let's consider a patterned string like before:
line = "xy"*5+" | "+"yz"*5
prefix = "xy"
line_new = line.removeprefix(prefix)
print("Before: ", line, line.count(prefix))
print("After: ", line_new, line_new.count(prefix))
This code removes the prefix "xy" of the first string and displays a number of occurrences of the prefix for both lines, at the end. Let's run the code:
$ python remove_prefix.py
Before: xyxyxyxyxy | yzyzyzyzyz 5
After: xyxyxyxy | yzyzyzyzyz 4
Similarly, the suffix can be removed using the removesuffix()
method:
line = "xy"*5+" | "+"yz"*5
suffix = "yz"
line_new = line.removesuffix(suffix)
print("Before: ", line, line.count(suffix))
print("After: ", line_new, line_new.count(suffix))
Running this code results in:
$ python remove_suffix.py
Before: xyxyxyxyxy | yzyzyzyzyz 5
After: xyxyxyxyxy | yzyzyzyz 4
Remove Prefix/Suffix in Python Versions < 3.9
In Python versions lesser than 3.9, this problem can be solved indirectly by using a combination of the startswith()
and endswith()
methods, mixed with slicing a string. This validates if the prefix/suffix is present and the corresponding segments can be snipped from the string.
Here's how the prefix can be easily removed from the string:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
line = "xy"*5+" | "+"yz"*5
prefix = "xy"
if line.startswith(prefix):
line_new = line[len(prefix):]
print("Before: ", line, line.count(prefix))
print("After: ", line_new, line_new.count(prefix))
After verifying that the prefix exists, we slice the string to contain everything but the prefix and return the results. This gives the following output where the prefix "xy" is removed from the string:
$ python remove_prefix_alt.py
Before: xyxyxyxyxy | yzyzyzyzyz 5
After: xyxyxyxy | yzyzyzyzyz 4
To remove the suffix, we just adjust the order of parameters in the slicing notation to iterate from the end of the string:
line = "xy"*5+" | "+"yz"*5
suffix = "yz"
if line.endswith(suffix):
line_new = line[:-len(suffix)]
print("Before: ", line, line.count(suffix))
print("After: ", line_new, line_new.count(suffix))
Again, we first validate the line using the endswith()
method to check if the suffix is present. If this is true, the suffix is removed from the string:
$ python remove_suffix_alt.py
Before: xyxyxyxyxy | yzyzyzyzyz 5
After: xyxyxyxyxy | yzyzyzyz 4
Conclusion
Removing prefixes and suffixes can be very helpful when it comes to performing text processing. Python v3.9+ comes with two new functions to make this possible: removeprefix()
and removesuffix()
. When we are using Python versions less than 3.9, we can use the startswith()
and endswith()
methods with string slicing to remove a prefix and suffix respectively.