Re: string replace
- From: Jim Segrave <jes@xxxxxxxxxxxx>
- Date: Fri, 30 Jun 2006 14:38:28 -0000
In article <1aapg.21924$_J1.275201@xxxxxxxxxxxxxxxxxx>,
Michele Petrazzo <michele.petrazzo@xxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxx)
.
- References:
- string replace
- From: Michele Petrazzo
- string replace
- Prev by Date: Chapter 9 Tutorial for Classes Not Working
- Next by Date: Re: print shell output in a file
- Previous by thread: Re: string replace
- Next by thread: RE: string replace
- Index(es):
Relevant Pages
|