Search code examples
pythonpython-3.xstrip

Why aren't python's strip and lstrip methods behaving as expected in this case?


I'm trying to remove some junk from messy CSV strings and I don't understand why strip and lstrip aren't behaving as expected.

I am expecting this to return "ABC" but can't seem to strip these characters.

>>> a = '","ABC"'
>>> a
'","ABC"'
>>> a.strip('",')
'ABC'
>>> a.lstrip('",')
'ABC"'

Solution

    • a.lstrip('",') removes all ','s and '"'s at the beginning of a. (Left strip.)
    • a.rstrip('",') removes all ','s and '"'s at the end of a. (Right strip.)
    • a.strip('",') removes all ','s and '"'s at both sides of a.