strip question

Steven D'Aprano steve at
Sat Jan 27 07:00:36 CET 2007

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

> hi
> can someone explain strip() for these :
> [code]
>>>> x=''
>>>> 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


It gets weirder:

>>> x.strip('s')

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

>>> x.strip('w')
>>> x.strip('o')
>>> x.strip('r')

Strips nothing.

>>> x.strip('ba')

Strips correctly.

>>> x.strip('bwa')

Strips the a and b but not the w.

>>> x.strip('bwas')

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

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

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:

    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



More information about the Python-list mailing list