Deleting specific characters from a string

drs drs at ecp.cc
Wed Jul 9 03:40:27 EDT 2003


"Behrang Dadsetan" <ben at dadsetan.com> wrote in message
news:begfb3$7j6$1 at online.de...
> Hi all,
>
> I would like deleting specific characters from a string.
> As an example, I would like to delete all of the '@' '&' in the string
> 'You are ben at orange?enter&your&code' so that it becomes
> 'benorange?enteryourcode'.
>
> So far I have been doing it like:
> str = 'You are ben at orange?enter&your&code'
> str = ''.join([ c for c in str if c not in ('@', '&')])
>
> but that looks so ugly.. I am hoping to see nicer examples to acheive
> the above..

you can use the replace method of the string object
>>>  'You are ben at orange?enter&your&code'.replace('@', '').replace('&', '')
'You are benorange?enteryourcode'

-d






More information about the Python-list mailing list