[Python-ideas] New explicit methods to trim strings

Anders Hovmöller boxed at killingar.net
Sun Mar 24 05:34:49 EDT 2019


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> wrote:
> 
> Following the discussion here 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'
> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190324/30e5a676/attachment.html>


More information about the Python-ideas mailing list