469,364 Members | 2,332 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

may be a bug in string.rstrip

Hi :

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

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.

Kyo.

Nov 23 '07 #1
2 1663
"kyo guan" <ky*****@gmail.comwrote in
news:ma***************************************@pyt hon.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

Nov 23 '07 #2
Ant
On Nov 23, 4:09 am, "kyo guan" <kyog...@gmail.comwrote:
....
>'120.exe'.rstrip('.exe')
Another approach since you seem to be working with filenames is using
the os.path module:
>>import os.path as path
s = "test.torrent"
t = "test.exe"
u = "test"
path.splitext(s)[0]
'test'
>>path.splitext(t)[0]
'test'
>>path.splitext(u)[0]
'test'
Nov 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hank | last post: by
11 posts views Thread by Helmut Jarausch | last post: by
4 posts views Thread by Andy | last post: by
3 posts views Thread by steve morin | last post: by
7 posts views Thread by Steven D'Aprano | last post: by
3 posts views Thread by Ameet Nanda | last post: by
2 posts views Thread by dirkheld | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.