Python 3.9.0 has introduced two new methods to work with strings:
As their names suggest, one is used to remove a prefix from a string while the other one is used to remove a suffix.
Given a string and a prefix, if the string begins with the prefix, the prefix is being removed, otherwise a copy of the original string is being returned:
In : 'MyStringExample'.removeprefix('My') Out: 'StringExample' In : 'MyStringExample'.removeprefix('Foo') Out: 'MyStringExample'
Given a string and a suffix, if the string ends with the suffix, the suffix is being removed, otherwise a copy of the original string is being returned:
In : 'ThisIsATest'.removesuffix('Test') Out: 'ThisIsA' In : 'ThisIsATest'.removesuffix('Foo') Out: 'ThisIsATest'
Of course these are not the only features which have been added to Python 3.9.0, so I may cover more in the next days. If in the mean time you have any preferences, please leave a comment below and thanks for reading.