Removing a Prefix or a Suffix in a String in Python

As of Python 3.9, the String type will have two new methods.

You can specifically remove a prefix from a string using the removeprefix() method:

>>> 'Rio de Janeiro'.removeprefix("Rio")
' de Janeiro'

Or remove a suffix using the removesuffix() method:

>>> 'Rio de Janeiro'.removesuffix("eiro")
'Rio de Jan'

Simply pass as argument the text to be considered as prefix or suffix to be removed and the method will return a new string as a result.

I recommend the reading of the PEP 616 in the official documentation if you are curious about how these features are added to the language.

This one is a pretty simple change and very friendly for beginners to get used to reading the official documentation.