removing characters from a string?

Alex Martelli alex at magenta.com
Tue Aug 1 03:39:05 EDT 2000


"Rikard Bosnjakovic" <bos at hack.org> wrote in message
news:3985F5DD.CB1BB073 at hack.org...
> jesse at multimediacollective.com wrote:
>
> > this_string = "jesse at multimediacollective.com"
> > """and I wanted to get rid of the @, how would I do that? This
>
> string.join(string.split(this_string, "@"), "")

Or, more directly, string.replace(this_string, '@', '').

However, repeated applications of these idioms are sub-optimal if the goal
is to remove all occurrences of all characters in a set, as the original
poster
suggested.  string.translate is optimal for that.

    identity=string.maketrans('','')    # no transformations required
    string.translate(this_string, identity, '@!$%{}')

This will perform exactly the stated request.


Alex







More information about the Python-list mailing list