PHP's str_replace ?

Grzegorz Staniak gstaniak at wp.pl
Wed Sep 10 15:57:36 CEST 2008


On 10.09.2008, Anjanesh Lekshminarayanan <mail at anjanesh.net> wroted:

> In PHP, if I do
> str_replace(array('a', 'e', 'i', 'o', 'u'), '-', $str)
> it'll replace all vowels with a hyphen in string $str.
>
> Is there some equivalent in Python ?

The .translate() method of strings?

>>> import string
>>> mystr = "This is just a test"
>>> transtable = string.maketrans("aeiouy","------")
>>> mystr.translate(transtable)
'Th-s -s j-st - t-st'


GS
-- 
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Nocturnal Infiltration and Accurate Killing



More information about the Python-list mailing list