strip question

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Jan 29 16:54:53 EST 2007


eight02645999 at yahoo.com a écrit :
> 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?

Probably because the Fine Manual(tm) says that str.strip() removes 
heading and trailing chars ?

"""
bruno at bibi ~ $ python
Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on 
linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> help(''.strip)
Help on built-in function strip:

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 characters in chars instead.
     If chars is unicode, S will be converted to unicode before stripping
"""


You may want to try str.replace() instead:

"""
 >>> help(''.replace)
Help on built-in function replace:

replace(...)
     S.replace (old, new[, count]) -> string

     Return a copy of string S with all occurrences of substring
     old replaced by new.  If the optional argument count is
     given, only the first count occurrences are replaced.
 >>> 'abcd,words.words'.replace(',', '').replace('.', '')
'abcdwordswords'
"""

> thanks

HTH




More information about the Python-list mailing list