Use String Remove Affix
Sourcery refactoring id: use-string-remove-affix
Description:
Replaces string slicing with the Python 3.9 features removesuffix
and
removeprefix
.
Before:
if text.startswith("Hello, "):
text = text[7:]
if text.endswith(" World!"):
text = text[: -len(" World!")]
After:
text = text.removeprefix("Hello, ")
text = text.removesuffix(" World!")
Explanation:
This refactoring is motivated by PEP 616. As stated in the documentation, using these methods confers several advantages, including being more robust to user error, more performant, and more descriptive.