Search code examples
pythonstringstrip

Python weird string processing issue with strip


Can anyone explain the following behavior to me? This doesn't make any sense.

I have a string:

In [170]: t = "c:\\website\\site-env\\scripts"

I try to strip "c:\website" from the front, and it doesn't work:

In [171]: t.strip("c:\\website")
Out[171]: '-env\\scrip'

I back up a little to see where it breaks:

In [172]: t.strip("c:\\websi")
Out[172]: 'te\\site-env\\script'

For some reason, it happens after the "t"!

In [173]: t.strip("c:\\websit")
Out[173]: '-env\\scrip'

Any ideas? I'm stumped.


Solution

  • strip uses the characters, not the string. For example, "abcdefg".strip("facedb") would result in "g". Use a slice instead:

    t[len(r'c:\website'):] if t.startswith(r'c:\website') else t