Caveat Emptor: Python rstrip()/lstrip()

Well this one is nasty, lets see:


>>> a="funky_monkey"
>>> a.rstrip("_monkey")
'fu'

A truly WTF? moment. But... string.rstrip() and string.lstrip() works with list of characters not strings. So what python does is treating the {r/l}strip() argument as a list of characters. So it goes over ['_', 'm', 'o', 'n', 'k', 'e', 'y'] list and starts chopping characters from the right as it finds one. If the last character at the right end doesn't match any characters in the list it stops.

Thats it for now folks.

0 yorum: