473,761 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with the strip string method

The Library Reference has
strip( [chars])

Return a copy of the string with the
leading and trailing characters removed.
The chars argument is a string
specifying the set of characters to be
removed. If omitted or None, the chars
argument defaults to removing
whitespace. The chars argument is not a
prefix or suffix; rather, all
combinations of its values are stripped:
>>' spacious '.strip()
'spacious'
>>'www.example. com'.strip('cmo wz.')
'example'

Only the last two examples below behave
as expected.

Is it intended that the full range of
characters be handled?

Colin W.

[Dbg]>>'ab$%\n\rcd'. strip('%')
'ab$%\n\rcd'
[Dbg]>>'ab$%cd'.stri p('$')
'ab$%\n\rcd'
[Dbg]>>'ab$%cd'.stri p('$')
'ab$%cd'
[Dbg]>>' ab$%cd '.strip('$')
' ab$%cd '
[Dbg]>>' ab$%cd '.strip('%')
' ab$%cd '
[Dbg]>>' spacious '.strip()
'spacious'
[Dbg]>>'www.example. com'.strip('cmo wz.')
'example'
Mar 2 '08 #1
3 2369
Colin J. Williams wrote:
Return a copy of the string with the
leading and trailing characters removed.
^^^^^^^^^^^^^^^ ^^^^^
Only the last two examples below behave
as expected.
They all looks OK to me.
[Dbg]>>'ab$%\n\rcd'. strip('%')
'ab$%\n\rcd'
No "%" at the beginning or end of string. Nothing changed.
[Dbg]>>'ab$%cd'.stri p('$')
'ab$%\n\rcd'
No "$" at the beginning or end of string. Nothing changed. I believe that
you didn't copy this from the standard input due to the presence of "\r\n"
on the answer...
[Dbg]>>'ab$%cd'.stri p('$')
'ab$%cd'
No "$" at the beginning or end of string. Nothing changed.
[Dbg]>>' ab$%cd '.strip('$')
' ab$%cd '
No "$" at the beginning or end of string. Nothing changed.
[Dbg]>>' ab$%cd '.strip('%')
' ab$%cd '
No "%" at the beginning or end of string. Nothing changed.

Mar 2 '08 #2
Colin J. Williams wrote:
The Library Reference has
strip( [chars])

Return a copy of the string with the
leading and trailing characters removed.
The chars argument is a string
specifying the set of characters to be
removed. If omitted or None, the chars
argument defaults to removing
whitespace. The chars argument is not a
prefix or suffix; rather, all
combinations of its values are stripped:
>>' spacious '.strip()
'spacious'
>>'www.example. com'.strip('cmo wz.')
'example'

Only the last two examples below behave
as expected.
Adjust your expectations. The software is correct.
Is it intended that the full range of
characters be handled?

Colin W.

[Dbg]>>'ab$%\n\rcd'. strip('%')
'ab$%\n\rcd'
[Dbg]>>'ab$%cd'.stri p('$')
'ab$%\n\rcd'
[Dbg]>>'ab$%cd'.stri p('$')
'ab$%cd'
[Dbg]>>' ab$%cd '.strip('$')
' ab$%cd '
[Dbg]>>' ab$%cd '.strip('%')
' ab$%cd '
[Dbg]>>' spacious '.strip()
'spacious'
[Dbg]>>'www.example. com'.strip('cmo wz.')
'example'
I suspect what you need is the .replace() method.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Mar 2 '08 #3
Thanks to all respondents, Steve Holden
is right, I expected more than I should
have.
Others have explained why all your examples work as they should.
From your exmaples, it seems like you would like strip to
remove the leading and trailing characters from EVERY LINE in
your string. This can be done by the simple construct
>>my_string = ' foo \n bar '
'\n'.join(lin e.strip() for line in my_string.split ('\n'))
'foo\nbar'

If you need this construct at several places, define a function

def line_strip(stri ng,sep='\n') :
return sep.join(line.s trip() for line in string.split(se p))

cheers,

- harold -
Mar 3 '08 #4

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

Similar topics

4
4461
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using python 2.3 Here is an exemple of the piece of code I use: import urllib2 #Proxy handler proxy_handler = urllib2.ProxyHandler({"http" :
3
2220
by: Robert Oschler | last post by:
Hello, I am using the following function to try and strip both carraige returns and line feeds, ASCII 13 and 10 respectively, from a string. It doesn't seem to be working: x = filter(lambda c: c not in "\012\015", string.strip(x)) # octal version I also tried:
16
10512
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am using C# eg. - String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from the database. - I do not care about the time portion, I only want "1/1/2005" for display.
2
2132
by: Mark | last post by:
I got this code from the Internet. Demo on the site works fine but downloaded version is not. I am a very new to .NET, I cannot figure this out, please help. Here is the code: ASPX code: <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %><%@ Page language="c#" Codebehind="DocTree.aspx.cs" AutoEventWireup="false" Inherits="shark.TreeView.DocTree" %><!DOCTYPE HTML PUBLIC...
6
2587
by: rtilley | last post by:
s = ' qazwsx ' # How are these different? print s.strip() print str.strip(s) Do string objects all have the attribute strip()? If so, why is str.strip() needed? Really, I'm just curious... there's a lot don't fully understand :)
6
2358
by: eight02645999 | last post by:
hi can someone explain strip() for these : 'example' when i did this: 'abcd,words.words'
4
4202
by: Ethan Furman | last post by:
Greetings. The strip() method of strings works from both ends towards the middle. Is there a simple, built-in way to remove several characters from a string no matter their location? (besides .replace() ;) For example: ..strip --'www.example.com'.strip('cmowz.') 'example' ..??? ----- 'www.example.com'.strip('cmowz.')
0
171
by: =?iso-8859-1?q?C=E9dric_Lucantis?= | last post by:
Hi, I don't see any string method to do that, but you can use a regexp : 'exaple' -- Cédric Lucantis
4
2287
by: Poppy | last post by:
I'm using versions 2.5.2 and 2.5.1 of python and have encountered a potential bug. Not sure if I'm misunderstanding the usage of the strip function but here's my example. var = "detail.xml" print var.strip(".xml") ### expect to see 'detail', but get 'detai' var = "overview.xml" print var.strip(".xml") ### expect and get 'overview' I have a work around using the replace function which happens to be the
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9765
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6603
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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 we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.