473,545 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

may be a bug in string.rstrip

Hi :

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

but this is a right answer:
>>'120.exe'.rst rip('.exe')
'120' <------ this is a right value.

there is a bug in the rstrip, lstrip there isn't this problem.


Nov 23 '07 #1
2 1879
"kyo guan" <ky*****@gmail. comwrote in
news:ma******** *************** *************** *@python.org:
Hi :

Please look at this code:
>>>'exe.torrent '.rstrip('.torr ent')
'ex' <----- it should be 'exe', why?
It really shouldn't be.
but this is a right answer:
>>>'120.exe'.rs trip('.exe')
'120' <------ this is a right value.

there is a bug in the rstrip, lstrip there isn't this
It's not a bug, but a misunderstandin g of the way the function

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".rstri p('.ex') or
"120.exe".rstri p('x.e') or
"120.exe".rstri p('ab.cdefghijk lmnopqrstuvwxyz ')

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.


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 >>> mystr = "test.txt" >>> mystr = string.rstrip(mystr, ".txt")
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) continually errors with: NameError: global name 'string' is not defined args = ("global name 'string' is not defined",) when run from the server CGI-BIN. ...
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 ...
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 ' The names are analogous to strip, rstrip, and lstrip. But the functionality is basically this: def lchop(self, prefix):
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
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: Float String 1.0 1
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': u'8'} Thanks, Matthew
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 authentication , I tried reading docs online which speak of something called as FancyUrlOpener. Now i want to hardcode my username and password inside the...
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 python 2.5.1, this same code fails to work on an ubuntu server with python 2.4.3 I was wondering if there is a problem with .rstrip() in that python...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.