bizarre behavior using .lstrip

Jules Dubois bogus at invalid.tld
Sat Sep 20 06:23:54 CEST 2003


On 19 Sep 2003 17:07:47 -0700, in article
<681f9321.0309191607.4e8b858a at posting.google.com>, Pete Jereb wrote:

>>>> s = 'chg conn_fee'
>>>> print s
> chg conn_fee
>>>> print s.lstrip('chg ')
> onn_fee
> 
> Does this make any sense at all?  where did the lead c in conn_fee go?

It was removed, as you requested.  lstrip('chg ') means remove every
occurence of space, 'g', 'h', and 'c' from the front (left) of the string.

>>>> print s.lstrip('chg')
>  conn

lstrip('chg') stops stripping when it finds a character not in 'chg'.  The
space is the first character not 'chg' so that's where lstrip stops
stripping.

>>>> print s.lstrip('chg ')
> onn

lstrip('chg ') stops stripping when it finds a character not in 'chg '.
The 'o' is the first character not in 'chg' so that's where lstrip stops
stripping.

> Not really sure what's causing this [...]

As they said where I used to work, WAD: it Works As Designed.

> [...] but it's making me change the
> text parser  I'm 600 lines into.

It's not clear what you're trying to do.  Do you want to remove all the
leading space, 'g', 'h' and 'c' characters from the string, or do you want
to remove the leading (exact) string "chg " as a whole?

One way to remove the leading 'g', 'h', and 'c' characters from the front
of the string first, and then remove spaces from the resulting string

  print s.lstrip('chg').lstrip()




More information about the Python-list mailing list