[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Problem with the strip string method

Colin J. Williams

3/2/2008 4:37:00 PM

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

Jorge Godoy

3/2/2008 4:50:00 PM

0

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.

Martin Blume

3/2/2008 4:56:00 PM

0

"Colin J. Williams" schrieb
> 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".

>>> "xxxaaaxxx".strip("x")
'aaa'
>>> "xxxaaaxxxaaaxxx".strip("x")
'aaaxxxaaa'
>>>

HTH
Martin



Steve Holden

3/2/2008 5:46:00 PM

0

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.hold...

Aaron Brady

3/2/2008 6:17:00 PM

0

On Mar 2, 11:45 am, Steve Holden <st...@holdenweb.com> wrote:
> I suspect what you need is the .replace() method.

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

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

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).

Colin J. Williams

3/2/2008 7:00:00 PM

0

castironpi@gmail.com wrote:
> On Mar 2, 11:45 am, Steve Holden <st...@holdenweb.com> wrote:
>> I suspect what you need is the .replace() method.
>
> The information's there-- the word 'contiguous' might clear it up a
> bit.
>
>>> 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:
>
> 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.

dadapapa

3/3/2008 3:05:00 PM

0

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