To strip at most 1 character from the end:
txt[:-1] + txt[-1:].rstrip(chars)
To strip at most N characters:
txt[:-N] + txt[-N:].rstrip(chars)
Given that, I think yours is too much of a niche case to change anything
in Python
Rob Cliffe
On 19/05/2020 12:44, computermaster360 . 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
I would appreciate if str.split, str.lstrip, str.rsplit functions had a
`maxstrip` argument, similar to the `maxsplit` argument of `str.split`, which
would specify the maximum count of characters to be removed from the string. In
case of `str.split` this maximum would apply to each side of the string
separately.
Am I the only one who is surprised such functionality is not implemented
already??
_______________________________________________
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/NSUFM4YA6E65ASDPJZPSJWBS2XEDRDFU/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
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/R5MV36GXCYPU2VEMNHOS3VWXB4AOJXOM/
Code of Conduct: http://python.org/psf/codeofconduct/