[Python-ideas] adding a trim convenience function

Erick Tryzelaar idadesub at users.sourceforge.net
Wed Mar 5 23:11:56 CET 2008


On Wed, Mar 5, 2008 at 1:30 PM, Matthew Russell <matt.horizon5 at gmail.com> wrote:
> Of couse that should of read:
> sites = list(item.strip("http://") for item in sites)

{l,r,}strip doesn't actually do what I'm talking about, which confused
me for a long time. Consider this simple case:

>>> 'abaaabcd'.lstrip('ab')
'cd'

ltrim in this case would produce 'aaabcd'.

On Wed, Mar 5, 2008 at 1:33 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> >>> [site.replace('http://', '')for site in sites]
> ['www.google.com', 'python.org', 'www.yahoo.com']

Unfortunately that would break down in certain cases with a different
strings, like 'foo bar foo'.replace('foo', ''), which just results in
' bar '.

>  Try another use case.  I think str pretty well has the basic tools needed
>  to construct whatever specific tools one needs.

Oh sure it can, considering that I can implement ltrim in three lines.
This is just to reduce a common pattern in my code, and to remove
rewriting it in multiple projects.



More information about the Python-ideas mailing list