New String Methods to Remove Prefixes and Suffixes in Python 3.9

removeprefix() vs lstrip(), removesuffix vs rstrip()

Photo by Vlada Karpovich from Pexels

New string methods in Python 3.9

In Python 3.9, new string methods removeprefix() and removesuffix() are introduced.

Topics covered in this story

removeprefix() vs lstrip()
removesuffix() vs rstrip()
Different ways to remove prefix and suffix before Python 3.9


removeprefix() vs lstrip()

removeprefix()

str.removeprefix(prefix)

  • If the string starts with the prefix string, return string[len(prefix):]
    Otherwise, return a copy of the original string.
  • The parameters of removeprefix() are considered as a substring and not as a set of characters.

lstrip()

str.lstrip([chars])

  • Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed.chars argument is not a prefix, rather all combinations of its value are stripped-Python docs

Example 1: Remove prefix “abc” from the string

Image by Author
s1="abcPYTHONabc"
print (s1.removeprefix("abc"))
#Output:PYTHONabc
print (s1.lstrip("abc"))
#Output:PYTHONabc

Example 2: The parameters of removeprefix() are considered as a substring. But parameters of lstrip() are considered as a set of characters.

s1="abcbcacPYTHONabc"
print (s1.removeprefix("abc"))
#Output:bcacPYTHONabc
print (s1.lstrip("abc"))
#Output:PYTHONabc

Example 3: removeprefixwon’t remove multiple copies of a prefix. But lstrip() will remove multiple copies of a prefix

s1="abcabcPYTHONabc"
print (s1.lstrip("abc"))
#Output:PYTHONabc
print (s1.removeprefix("abc"))
#Output:abcPYTHONabc

Example 4: If the parameter is not mentioned in lstrip(), it will remove leading whitespaces. But if the parameter is not mentioned in removeprefix(), it will raise TypeError

s1="   PYTHON"
print (s1.lstrip())
#Output:PYTHON
print (s1.removeprefix())
#Output:TypeError: str.removeprefix() takes exactly one argument (0 given)

Example 5: If the parameter mentioned is not found at the beginning of the string means, both methods will return a copy of the string

s1="PYTHONabc"
print (s1.lstrip("abc"))
#Output:PYTHONabc
print (s1.removeprefix("abc"))
#Output:PYTHONabc

removesuffix() vs rstrip()

removesuffix()

str.removesuffix(suffix)

  • If the string ends with the suffix string and that suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string:
  • The parameters of removesuffix() are considered as a substring and not as a set of characters.

rstrip()

str.rstrip([chars])

Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped.-Python docs

Example 1:Remove prefix “abc” from the string

Image by Author
s1="abcPYTHONabc"
print(s1.rstrip("abc"))
#Output:abcPYTHON
print(s1.removesuffix("abc"))
#Output:abcPYTHON

Example 2: The parameters of removesuffix() are considered as a substring. But parameters of rstrip() are considered as a set of characters.

Image by Author
s1="abcPYTHONacabc"
print(s1.rstrip("abc"))
#Output:abcPYTHON
print(s1.removesuffix("abc"))
#Output:abcPYTHONac

Example 3: removesuffixwon’t remove multiple copies of suffix. But rstrip() will remove multiple copies of the suffix.

Image by Author
s1="abcPYTHONabcabc"
print(s1.rstrip("abc"))
#Output:abcPYTHON
print(s1.removesuffix("abc"))
#Output:abcPYTHONabc

Example 4: If the parameter is not mentioned in rstrip(), it will remove trailing whitespaces. But if the parameter is not mentioned in removesuffix(), it will raise TypeError

s1="PYTHON   "
print(s1.rstrip())
#Output:PYTHON
print(s1.removesuffix())
#Output:TypeError: str.removesuffix() takes exactly one argument (0 given)

Example 5: If the parameter mentioned is not found at the end of the string means, both methods will return a copy of the string

s1="abcPYTHON"
print(s1.rstrip("abc"))
#Output:abcPYTHON
print(s1.removesuffix("abc"))
#Output:abcPYTHON

Different ways to remove prefix and suffix before Python 3.9

1. Using re.sub()

Removing suffix

import re
pattern=r"abc$"
s1="abcPYTHONacbcabc"
s2=re.sub(pattern,"",s)1
print (s2)
#Output:abcPYTHONacbc

pattern=r”abc$” → Checks whether the string ends with “abc”
$ →indicates the end of the string.

s2=re.sub(pattern,””,s1) → If the pattern matches, it will replace the substring with an empty string.

Removing prefix

import re
pattern=r"^abc"
s1="abcbcPYTHONabc"
s2=re.sub(pattern,"",s1)
print (s2)
#Output:bcPYTHONabc

pattern=r”^abc” → checks whether the string starts with “abc”

^ → indicates the beginning of the string

s2=re.sub(pattern,””,s1) → If the pattern matches, it will replace the substring with an empty string.

2. Using str.startswith()

s1="abcbcacPythonacbcabc"
if (s1.startswith("abc")):
    print (s1.replace("abc","",1))

#Output:bcacPythonacbcabc

If the string starts with “abc” means it will replace that substring by “” (empty string)
count =1 means only one occurrence of that substring mentioned is replaced by an empty string.

3. Using str.endswith()

s2="Pythonacbcabc"
if (s2.endswith("abc")):
    print (s2[:-3])

#Output:Pythonacbc
Image by Author

In python 3.9, removesuffix() and removeprefix() methods make it simpler to remove suffix and prefix from the string.

Conclusion

  1. The parameter for lstrip and rstrip is interpreted as a set of characters, not a substring. But the parameter for removeprefix and removesuffix is interpreted as a substring and not as a set of characters.
  2. removeprefix,removesuffix → only at most one copy of prefix/suffix is removed.
    lstrip,rstrip → the characters are repeatedly removed from the beginning/end of the string.
  3. The parameter for removeprefix() and removesuffix() is mandatory.But for lstrip() and rstrip(),it’s not mandatory.

Resources

PEP-616

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s