strip question

Daniel Klein danielkleinad at gmail.com
Tue Jan 30 20:20:51 CET 2007


On 26 Jan 2007 21:33:47 -0800, eight02645999 at yahoo.com 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

If you only have a couple of characters to deal with then use
replace(). Otherwise use string.translate() :

>>> import string
>>> x = 'abcd,words.words'
>>> transform = string.maketrans(',.','..')
>>> x = string.translate(x, transform)
>>> x = x.replace('.','')
>>> x
'abcdwordswords''


Dan



More information about the Python-list mailing list