Search code examples
pythonstringstrip

Why str.lstrip will truncate letter e for words beginning with e but not for other letters?


Strangely I met with this problem when I was trying to take out contents from an excel file with Python using xlrd modulus.

The example is like below:

Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a="text:u'ethos'"
>>> a
"text:u'ethos'"
>>> a.lstrip("text:u'").rstrip("'")
'hos'
>>> b="text:u'weekend'"
>>> b
"text:u'weekend'"
>>> b.lstrip("text:u'").rstrip("'")
'weekend'

a cell reading out from xlrd is in text:u'' format for I need to get only the word.

ALthough I finally use table.cell_value() method to solve it. But I wonder why?

So why 'weekend' is working well but for word like 'ethos' is wrong?


Solution

  • You should take a closer look at the lstrip function https://docs.python.org/2/library/stdtypes.html#str.lstrip

    Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

    It ends up stripping out the characters t, e, x, :, and u until it reaches a different character. For "text:u'ethos'", every character up to 'h' is in that list, so it removes them. With "text:w'weekend'", since 'w' is not in that list it stops stripping characters there.