Problem with the strip string method

Colin J. Williams fn681 at ncf.ca
Sun Mar 2 11:37:15 EST 2008


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'



More information about the Python-list mailing list