Re: string replace



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)

.



Relevant Pages

  • Re: [Emacs] Kommentieren
    ... ;; completely up to the user to decide, what the string ... "Chars preserved of STRING. ... `CHARS-PRESERVE' must be a parentized expression, ...
    (de.comp.editoren)
  • Re: FASTEST way to try all strings (a until ZZZZZZZZZZZZZZZZZZZZZZZZ)
    ... > It will be a very huge table so I in my opinion. ... > When it would be used, than it should be converted to a string, however ... >> How would an array of Byte be any faster then an array of Char? ... >> array of Byte is needed, however the OP suggested Chars (A to Z, a to z ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FASTEST way to try all strings (a until ZZZZZZZZZZZZZZZZZZZZZZZZ)
    ... > It will be a very huge table so I in my opinion. ... > When it would be used, than it should be converted to a string, however ... >> How would an array of Byte be any faster then an array of Char? ... >> array of Byte is needed, however the OP suggested Chars (A to Z, a to z ...
    (microsoft.public.dotnet.general)
  • Re:(9corr) string
    ... and I want to remove the month in the timestamp for each of the string ... Then advance 4 chars, and copy from +2 to the current pointer until you ... pointer past the year part to reach the month part. ... this guarantee is 'memmove'. ...
    (comp.lang.c)
  • Re: speed question
    ... chars of column A and first 2 of column B. ... as it is a string contatenation. ... > I want to have smart search like iTunes or something like that. ... > Dzemal Tipura (Dzemo) - MCP ...
    (microsoft.public.dotnet.framework.adonet)