strings getting unnecessarily cut in lstrip

M

Maxim Mercury

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

Steve Holden

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top