Skip to content Skip to sidebar Skip to footer

Are There Limits To Using String.lstrip() In Python?

Possible Duplicate: Is this a bug in Python 2.7? The .lstrip() function doesn't quite work as I expected on some strings (especially ones with underscores). e.g., In [1]: a='

Solution 1:

.lstrip() doesn't do what you think it does. It removes any of the provided characters from the left end of the string. The second underscore is as much an underscore as the first, so it got removed too.

"aaaaaaaabbbbbbbc".lstrip("ab")  # "c"

Solution 2:

What you want:

b = 'abcd_efg'if a.startswith(b):
    a = a[len(b):]

As the str.lstrip documentation says,

The chars argument is not a prefix; rather, all combinations of its values are stripped:

>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

Solution 3:

To do what you want:

>>>a = "foobar">>>sub = "foo">>>b = a[len(sub):]>>>b
'bar'

Post a Comment for "Are There Limits To Using String.lstrip() In Python?"