strings getting unnecessarily cut in lstrip

Steve Holden steve at holdenweb.com
Sun Nov 21 22:26:49 EST 2010


On 11/21/2010 10:12 PM, Maxim Mercury wrote:
> I was using the lstrip to trim the left occurance of a string, and it
> didnt seem to work for some cases. but when i investigated there does
> seem to be an issue with some combination of strings
> here is one case
>  p1 = "abcd"
>  p2 = 'def'                    # $abc
>  sym = '_'
>  str1 = p1 + sym + p2
>  str1 = str1.lstrip(p1+sym)
>  print str1                             # this prints ef instead of
> def
> 
> can someone explain why this is happening ?

It's happening because the argument specifies a set of characters, not a
string. So the "d" of "def" is removed because there's a "d" in "abcd_".

If you want to remove a string, try testing is with its .startswith()
method and them removing the right number of characters.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17       http://us.pycon.org/
See Python Video!       http://python.mirocommunity.org/
Holden Web LLC                 http://www.holdenweb.com/




More information about the Python-list mailing list