FAQ
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 ?

Search Discussions

  • Steve Holden at Nov 22, 2010 at 3:26 am

    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/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 22, '10 at 3:12a
activeNov 22, '10 at 3:26a
posts2
users2
websitepython.org

2 users in discussion

Steve Holden: 1 post Maxim Mercury: 1 post

People

Translate

site design / logo © 2022 Grokbase