string replace

Jim Segrave jes at nl.demon.net
Fri Jun 30 10:38:28 EDT 2006


In article <1aapg.21924$_J1.275201 at twister2.libero.it>,
Michele Petrazzo  <michele.petrazzo at TOGLIunipex.it> wrote:
>Hi,
>a lot of times I need to replace more than one char into a string, so I
>have to do something like
>
>value = "test"
>chars = "e"
>for c in chars:
>   value = value.replace(c, "")
>
>A solution could be that "replace" accept a tuple/list of chars, like
>that was add into the new 2.5 for startswith.
>
>I don't know, but can be this feature included into a future python release?

Let's say you want to make every vowel uppercase:

import string

trans_table = string.maketrans('aeiou', 'AEIOU')
"I don't know, but can be this feature included into".translate(trans_table)

prints:
"I dOn't knOw, bUt cAn bE thIs fEAtUrE InclUdEd IntO"

That more than addresses your requirements, as it can do multiple
character substitutions multiple times in one call.




-- 
Jim Segrave           (jes at jes-2.demon.nl)




More information about the Python-list mailing list