string replace

johnzenger at gmail.com johnzenger at gmail.com
Fri Jun 30 14:55:54 EDT 2006


Check out the .translate method and the string.maketrans documentation.
 You can use it to delete a list of characters all in one line:

>>> s = "I am the walrus"
>>> import string
>>> s.translate(string.maketrans("",""),"aeiou")
'I m th wlrs'



Michele Petrazzo 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?
> 
> Thanks,
> Michele




More information about the Python-list mailing list