Vijayendra Bapte Vijayendra Bapte - 3 days ago 4
Python Question

Why does str.lstrip strips an extra character?

Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> path = "/Volumes/Users"
>>> path.lstrip('/Volume')
's/Users'
>>> path.lstrip('/Volumes')
'Users'
>>>


I am expecting output of
path.lstrip('/Volumes')
should be
/Users

Answer

lstrip is character-based, it removes all characters from the left end that are in that string.

To verify this, try this:

"/Volumes/Users".lstrip("semuloV/")

Since / is part of the string, it is removed.

I suspect you need to use slicing instead:

if s.startsWith("/Volumes"):
    s = s[8:]

but hopefully someone with more intimate knowledge of the Python library might give you a better option.

Comments