strings getting unnecessarily cut in lstrip

Discussion in 'Python' started by Maxim Mercury, Nov 22, 2010.

  1. 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 ?
     
    Maxim Mercury, Nov 22, 2010
    #1
    1. Advertising

  2. Maxim Mercury

    Steve Holden Guest

    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/
     
    Steve Holden, Nov 22, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Melchior

    Detecting unnecessarily jar in classpath

    Stephan Melchior, Feb 15, 2005, in forum: Java
    Replies:
    1
    Views:
    320
    Wendy S
    Feb 15, 2005
  2. Pete Jereb

    bizarre behavior using .lstrip

    Pete Jereb, Sep 20, 2003, in forum: Python
    Replies:
    8
    Views:
    339
    Peter Hansen
    Sep 20, 2003
  3. Replies:
    2
    Views:
    624
    Jonathan N. Little
    May 25, 2007
  4. BillE
    Replies:
    2
    Views:
    418
    BillE
    Nov 12, 2008
  5. poliklosio
    Replies:
    1
    Views:
    263
    Alf P. Steinbach /Usenet
    Jun 28, 2010
Loading...

Share This Page