str.removeprefix
In python 3.9, PEP-616 introduced str.removeprefix
and str.removesuffix
methods:
'abcd'.removeprefix('ab')
# 'cd'
'abcd'.removeprefix('fg')
# 'abcd'
The implementation is simple (it's implemented on C, of course, but the idea is the same):
def removeprefix(self: str, prefix: str) -> str:
if self.startswith(prefix):
return self[len(prefix):]
return self