[Python-ideas] New explicit methods to trim strings

Alex Grigoryev evrial at gmail.com
Sun Mar 24 04:42:39 EDT 2019


Following the discussion here (https://link.getmailspring.com/link/7D84D131-65B6-4EF7-9C43-51957F9DFAA9@getmailspring.com/0?redirect=https%3A%2F%2Fbugs.python.org%2Fissue36410&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D) I propose to add 3 new string methods: str.trim, str.ltrim, str.rtrim
Another option would be to change API for str.split method to work correctly with sequences.

In [1]: def ltrim(s, seq):
...: return s[len(seq):] if s.startswith(seq) else s
...:

In [2]: def rtrim(s, seq):
...: return s[:-len(seq)] if s.endswith(seq) else s
...:

In [3]: def trim(s, seq):
...: return ltrim(rtrim(s, seq), seq)
...:

In [4]: s = 'mailto:maria at gmail.com'

In [5]: ltrim(s, 'mailto:')
Out[5]: 'maria at gmail.com'

In [6]: rtrim(s, 'com')
Out[6]: 'mailto:maria at gmail.'

In [7]: trim(s, 'm')
Out[7]: 'ailto:maria at gmail.co'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190324/27e121fe/attachment.html>


More information about the Python-ideas mailing list