Deleting specific characters from a string

DG david at fielden.com.au
Wed Jul 9 09:54:52 CEST 2003


>>> import string
>>> str = 'You are ben at orange?enter&your&code'
>>> print string.replace(string.replace(str, '@', ''), '&', '')
You are benorange?enteryourcode
>>>

Rowdy


> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Behrang Dadsetan
> Sent: Wednesday, 9 July 2003 5:12 PM
> To: python-list at python.org
> Subject: Deleting specific characters from a string
> 
> 
> 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..
> 
> Thanks.
> Ben.
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list