Problem with the strip string method

Steve Holden steve at holdenweb.com
Sun Mar 2 18:45:50 CET 2008


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.holdenweb.com/




More information about the Python-list mailing list