Skip to content Skip to sidebar Skip to footer

Why I Am Getting These Different Outputs?

print('xyxxyyzxxy'.lstrip('xyy')) # output:zxxy print('xyxefgooeeee'.lstrip('efg')) # ouput:xyxefgooeeee print('reeeefooeeee'.lstrip('eeee')) # output:reeeefooeeee Here for the

Solution 1:

In Python leading characters in Strings containing xyy are removed because of .lstrip(). For example:

txt = ",,,,,ssaaww.....banana"

x = txt.lstrip(",.asw")

print(x)

The output will be: banana

Solution 2:

string.lstrip(chars) removes characters from the left size of the string until it reached a character that does not appear in chars.

In your second and third examples, the first character of the string does not appear in chars, so no characters are removed from the string.

Solution 3:

I think because the order of char doesn't matter.

xyy or yxx will result in the same thing. It will remove chars from the left side until it sees a char that is not included. For example:

print('xyxxyyzxxy'.lstrip('xyy'))
zxxy

print('xyxxyyzxxy'.lstrip('yxx'))
zxxy

In fact, if you only used 2 chars 'xy' or 'yx' you will get the same thing:

print('xyxxyyzxxy'.lstrip('xy'))
zxxy

In the other cases, the first left char is not included, therefore there's no stripping

Solution 4:

lstring using the set of the chars in the string and then removes the all characters from the primary string start from the left

print('xyxefgooeeee'.lstrip('yxefg')) 
"""In 'xyxefgooeeee' the first char is 'x' and it exists in the 'yxefg' so 
will be removed and then it will move to the next char 'y','x','x','e','f', 
'g' and then 'o' which doesn't exist. therefore will return string after 'o'
"""
OutPut : ooeeee

print('xyxefgooeeee'.lstrip('efg'))
"""In the xyxefgooeeee' the first char 'x' does to exist in the 'efg' so will
not be removed and will not move to the next char and will return the
entire primary string
"""
OutPut: xyxefgooeeee

Post a Comment for "Why I Am Getting These Different Outputs?"