Deleting characters from a string

Magnus L. Hetland mlh at
Sat Jun 5 21:51:05 CEST 1999

Hrvoje Niksic <hniksic at> writes:

> string.translate can delete characters from a string, as well as
> translate them.  But what if I want *only* to delete them?  Currently
> I do this:
> dummytable = string.maketrans('', '')
> ...
>   host = string.translate(host, dummytable, ' "')
> ...
> What I would really like to be able to do is write:
>   host = string.translate(host, None, ' "')

Just to butt in with my private opinion... :) I think it would be more
natural if bot the last two arguments were optional... Thus you could

    host = string.translate(host, deletechars=' "')

To me that is more readable, and seems more like standard Python than
defining None to mean an ID-mapping... Just use an ID-mapping as a
default argument in string.translate.

> Am I missing a more elegant solution?

result = ""
for char in host:
    if char not in ' "':
        result = result+char

host = result

What!? To me that's more elegant... <wink>


  Magnus              Making no sound / Yet smouldering with passion
  Lie          The firefly is still sadder / Than the moaning insect
  Hetland                                       : Minamoto Shigeyuki

More information about the Python-list mailing list