On Tue, May 19, 2020 at 2:58 PM computermaster360 . < computermaster...@gmail.com> wrote:
> I often find myself in a need of stripping only a specified amount of > characters from a string (most commonly a single character). I have been > implementing this in an ad-hoc manner, which is quite inelegant: > > def rstrip1(txt, chars): > if txt is None: > return None > elif any(txt.endswith(c) for c in chars): > return txt[:-1] > else: > return txt > If you want to remove at most one instance of one specific character, then I believe in Python 3.9 you will be able to write `txt.removesuffix(char)` - see https://www.python.org/dev/peps/pep-0616/. For removing one of several possible characters, the potential API for that has currently been rejected: https://www.python.org/dev/peps/pep-0616/#accepting-a-tuple-of-affixes
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/LPME7YJEL2YOTSAV3ON4QEWJ3NHT5QJ6/ Code of Conduct: http://python.org/psf/codeofconduct/