Strange bug in string.strip () using python 2.7 and 3.5

advertisements

This question already has an answer here:

  • Python string.strip stripping too many characters 2 answers

I am getting some very odd result on a quite simple string manipulation using string.strip(). I am wondering whether it's a problem that affects only me (something is wrong with my python installations?) or it's a common bug?

The bug is very wired and here it goes:

>>> a = './omqbEXPT.pool'
>>> a.strip('./').strip('.pool')
'mqbEXPT' #the first 'o' is missing!!!

It occurs only if an 'o' is following './' !

>>> a = './xmqbEXPT.pool'
>>> a.strip('./').strip('.pool')
'xmqbEXPT'

What's going on here?! I have tested this on both python 2.7 and 3.5 and the result doesn't change.


This is how the strip method is actually designed to work.

The chars argument is a string specifying the set of characters to be removed.

The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

So when you say my_string.strip('.pools'), it's going to remove all leading and trailing characters in that set (ie. {'.', 'p', 'o', 'l', 's'}).

You probably want to use str.replace or re.sub.

>>> './omqbEXPT.pool'.replace('./', '').replace('.pool', '')
'omqbEXPT'

>>> import re
>>> re.sub(r'^\.\/|\.pool$', '', './omgbEXPT.pool')
'omqbEXPT'