473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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('cmowz.')
'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'.strip('$')
'ab$%\n\rcd'
[Dbg]>>'ab$%cd'.strip('$')
'ab$%cd'
[Dbg]>>' ab$%cd '.strip('$')
' ab$%cd '
[Dbg]>>' ab$%cd '.strip('%')
' ab$%cd '
[Dbg]>>' spacious '.strip()
'spacious'
[Dbg]>>'www.example.com'.strip('cmowz.')
'example'
Mar 2 '08 #1
3 2349
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'.strip('$')
'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'.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.
[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('cmowz.')
'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'.strip('$')
'ab$%\n\rcd'
[Dbg]>>'ab$%cd'.strip('$')
'ab$%cd'
[Dbg]>>' ab$%cd '.strip('$')
' ab$%cd '
[Dbg]>>' ab$%cd '.strip('%')
' ab$%cd '
[Dbg]>>' spacious '.strip()
'spacious'
[Dbg]>>'www.example.com'.strip('cmowz.')
'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(line.strip() for line in my_string.split('\n'))
'foo\nbar'

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

def line_strip(string,sep='\n') :
return sep.join(line.strip() for line in string.split(sep))

cheers,

- harold -
Mar 3 '08 #4

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

Similar topics

4
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...
3
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...
16
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...
2
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: <%@...
6
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......
6
by: eight02645999 | last post by:
hi can someone explain strip() for these : 'example' when i did this: 'abcd,words.words'
4
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...
0
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
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"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.