Problem with the strip string method

  • Thread starter Colin J. Williams
  • Start date
C

Colin J. Williams

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: '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'
 
J

Jorge Godoy

Colin said:
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.
 
M

Martin Blume

The Library Reference has
strip( [chars])

Return a copy of the string with the
leading and trailing characters removed.
^^^^^^^^^^^^^^^^^^^^

It's "leading and trailing", not
"leading, trailing or embedded".

HTH
Martin
 
S

Steve Holden

Colin said:
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:'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
 
C

castironpi

I suspect what you need is the .replace() method.

The information's there-- the word 'contiguous' might clear it up a
bit.

Return the string's substring from the first character not a member of
'chars' to the last such.

Remove contiguous leading and trailing members of 'chars'. If omitted
or None, 'chars' defaults over to the set of whitespace set( "\n\r\t
" ). (XXX TODO: ask Steve Reg Ex Guru this).
 
C

Colin J. Williams

The information's there-- the word 'contiguous' might clear it up a
bit.


Return the string's substring from the first character not a member of
'chars' to the last such.

Remove contiguous leading and trailing members of 'chars'. If omitted
or None, 'chars' defaults over to the set of whitespace set( "\n\r\t
" ). (XXX TODO: ask Steve Reg Ex Guru this).

Thanks to all respondents, Steve Holden
is right, I expected more than I should
have.

Colin W.
 
H

Harold Fellermann

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
'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 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top