strip question

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sat Jan 27 01:00:36 EST 2007


On Fri, 26 Jan 2007 21:33:47 -0800, eight02645999 wrote:

> hi
> can someone explain strip() for these :
> [code]
>>>> x='www.example.com'
>>>> x.strip('cmowz.')
> 'example'
> [/code]
> 
> when i did this:
> [code]
>>>> x = 'abcd,words.words'
>>>> x.strip(',.')
> 'abcd,words.words'
> [/code]
> 
> it does not strip off "," and "." .Why is this so?
> thanks


Fascinating...

It gets weirder:

>>> x.strip('s')
'abcd,words.word'

Why strip only the final s, not the earlier one?

>>> x.strip('w')
'abcd,words.words'
>>> x.strip('o')
'abcd,words.words'
>>> x.strip('r')
'abcd,words.words'

Strips nothing.

>>> x.strip('ba')
'cd,words.words'

Strips correctly.

>>> x.strip('bwa')
'cd,words.words'

Strips the a and b but not the w.

>>> x.strip('bwas')
'cd,words.word'

...and only one of the S's.

>>> y = "bwas"
>>> y.strip('bwas')
''
>>> y = "bwasxyz"
>>> y.strip('bwas')
'xyz'

And yet these work.


You know, I'm starting to think there may be a bug in the strip method...
either that or the documentation should say:

strip(...)
    S.strip([chars]) -> string or unicode

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove none, some or all characters in
    chars instead. If chars is unicode, S will be converted to unicode
    before stripping


*wink*


-- 
Steven.




More information about the Python-list mailing list