PEP 616 String methods to remove prefixes and suffixes ¶
See also
Articles ¶
https://martinheinz.dev ¶
math module is not the only one that got some new functions. Two new convenience functions for strings were added too:
removeprefix ¶
# Remove prefix
"someText".removeprefix("some")
# "Text"
removesuffix ¶
# Remove suffix
"someText".removesuffix("Text")
# "some"
These 2 functions perform what you would otherwise achieve using string[len(prefix):] for prefix and string[:-len(suffix)] for suffix.
These are very simple operations and therefore also very simple functions, but considering that you might perform these operations quite often, it’s nice to have built-in function that does it for you.