[Python-ideas] New explicit methods to trim strings

Alex Grigoryev evrial at gmail.com
Sun Mar 24 05:46:56 EDT 2019


Yeah good idea with names because php ltrim does the same as lstrip in python.
Normally I'd expect strip to behave as I proposed, not like input a string as mask of characters, which is more rare use case and confusing in some scenarios.
On мар т 24 2019, at 11:34 утра, Anders Hovmöller <boxed at killingar.net> wrote:
> I don't see what trim() is good for but I know I've written ltrim() hundreds of times easy.
>
> I propose naming them strip_prefix() and strip_suffix() and just skip the one that does both sides since it makes no sense to me.
>
> Trim is generally a bad name because what is called strip() in python is called trim() in other languages. This would be needlessly confusing.
>
> On 24 Mar 2019, at 09:42, Alex Grigoryev <evrial at gmail.com (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/0?redirect=mailto%3Aevrial%40gmail.com&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D)> wrote:
> > Following the discussion here (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/1?redirect=https%3A%2F%2Flink.getmailspring.com%2Flink%2F7D84D131-65B6-4EF7-9C43-51957F9DFAA9%40getmailspring.com%2F0%3Fredirect%3Dhttps%253A%252F%252Fbugs.python.org%252Fissue36410%26recipient%3DcHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%253D&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 (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/2?redirect=mailto%3Amaria%40gmail.com&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D)'
> >
> > In [5]: ltrim(s, 'mailto:')
> > Out[5]: 'maria at gmail.com (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/3?redirect=mailto%3Amaria%40gmail.com&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D)'
> >
> > In [6]: rtrim(s, 'com')
> > Out[6]: 'mailto:maria at gmail.'
> >
> > In [7]: trim(s, 'm')
> > Out[7]: 'ailto:maria at gmail.co (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/4?redirect=mailto%3Amaria%40gmail.co&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D)'
> > _______________________________________________
> > Python-ideas mailing list
> > Python-ideas at python.org (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/5?redirect=mailto%3APython-ideas%40python.org&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D)
> > https://mail.python.org/mailman/listinfo/python-ideas (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/6?redirect=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D)
> > Code of Conduct: http://python.org/psf/codeofconduct/ (https://link.getmailspring.com/link/5181B0DB-3B10-4202-90D6-1365AEF19654@getmailspring.com/7?redirect=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D)
> >
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190324/51edb43c/attachment-0001.html>


More information about the Python-ideas mailing list