Deleting specific characters from a string

Giles Brown giles_brown at hotmail.com
Thu Jul 10 03:50:53 EDT 2003


Behrang Dadsetan <ben at dadsetan.com> wrote in message news:<behssg$k5l$1 at online.de>...
>  >>> str = 'You are Ben at orange?enter&your&code'
>  >>> str.translate(string.maketrans('',''), '@&')
>  and
>  >>> str.replace('&', '').replace('@', '')
> are also ugly...

Well beauty is in the eye of the beholder they say.
Has anybody mentioned the re module yet?

>>> s = 'You are ben at orange?enter&your&code'
>>> import re
>>> re.sub('[@&]', '', s)
'You are benorange?enteryourcode'

Giles




More information about the Python-list mailing list