may be a bug in string.rstrip

rzed rzantow at gmail.com
Fri Nov 23 06:00:00 CET 2007


"kyo guan" <kyoguan at gmail.com> wrote in
news:mailman.1496.1195791004.13605.python-list at python.org: 

> Hi :
> 
>      Please look at this code:     
> 
>>>> 'exe.torrent'.rstrip('.torrent')
> 'ex'               <-----  it should be 'exe', why?

It really shouldn't be.

> 
> but this is a right answer:
> 
>>>> '120.exe'.rstrip('.exe')        
> '120'               <------ this is a right value.
> 
>      there is a bug in the rstrip, lstrip there isn't this
>      problem. 

It's not a bug, but a misunderstanding of the way the function
works.  

The argument you pass to strip, lstrip or rstrip is a character or
collection of characters to trim from the end of a string. You
would get the same results from: "120.exe".rstrip('.ex') or
"120.exe".rstrip('x.e') or
"120.exe".rstrip('ab.cdefghijklmnopqrstuvwxyz')

In other words, by passing ".torrent" as an argument, you cause
the function to remove and of the characters in this set: [.toren]
from the end of the string. Not surprisingly, it did remove
".torrent", but also the trailing 'e' from 'exe'. Since 'x' is not
in that set of characters, the function stopped there. 

-- 
rzed




More information about the Python-list mailing list