473,466 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1873
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hank | last post by:
Hi, I have this problem with string.rstrip Python 2.2.3 (#42, May 30 2003, 18:12:08) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import string >>>...
0
by: John F Dutcher | last post by:
Having 'cloned' an existing python script that imports 'string' and uses "string.rstrip()" without incident... I am at a loss to explain why the new 'cloned' script (brief sample below)...
11
by: Helmut Jarausch | last post by:
Hi, entering help('rstrip') or help('ljust') into IDLE's shell window I only get no Python documentation found ...
4
by: Andy | last post by:
What do people think of this? 'prefixed string'.lchop('prefix') == 'ed string' 'string with suffix'.rchop('suffix') == 'string with ' 'prefix and suffix.chop('prefix', 'suffix') == ' and ' ...
3
by: steve morin | last post by:
http://www.python.org/doc/2.4.1/lib/node110.html These methods are being deprecated. What are they being replaced with? Does anyone know? Steve
7
by: Steven D'Aprano | last post by:
I have a sinking feeling I'm missing something really, really simple. I'm looking for a format string similar to '%.3f' except that trailing zeroes are not included. To give some examples: ...
7
by: manstey | last post by:
Hi, How do I convert a string like: a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}" into a dictionary: b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS':...
3
by: Ameet Nanda | last post by:
Hi All, I access net using a proxy, which I have to authenticate everytime I try to access net from my system. Now when I use urllib2.urlopen(url) , I cant get ahead. I must provide proxy...
2
by: dirkheld | last post by:
Hi, I wrote some python code that retrieves urls from a txt file. In this code I use .rstrip() for removing the '\n' at the end of every url. While this code works on my mac (leopard) with...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.