Deleting characters from a string
Magnus L. Hetland
mlh at idt.ntnu.no
Sat Jun 5 15:51:05 EDT 1999
Hrvoje Niksic <hniksic at srce.hr> 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
do:
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