[Tutor] str.strip strange result...?

Peter Otten __peter__ at web.de
Fri Jan 15 11:37:25 EST 2016


Jignesh Sutar wrote:

> #python2.7
> 
>>>> s="V01_1"
>>>> s.strip("_1")
> 'V0'
> 
> 
> Wouldn't you expect the result to be "V01" ?

str.strip() doesn't strip off a suffix or prefix; its first argument is 
interpreted as a character set, i. e. as long as s ends/starts with any of 
the characters "_" or "1", remove that.

If you want to remove a suffix you have to write

if suffix and s.endswith(suffix):
    s = s[:-len(suffix)]



More information about the Tutor mailing list