strip question

James Stroud jstroud at mbi.ucla.edu
Sat Jan 27 01:04:06 EST 2007


Steven D'Aprano wrote:
> 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*
> 
> 

At the risk of appearing that I didn't notice your *wink*, the operative 
words in the docs would be "leading" and "trailing". But somehow I think 
you knew that.

James



More information about the Python-list mailing list