[Tutor] need a example of translate or maketrans

Terry Carroll carroll at tjc.com
Tue May 10 02:08:23 CEST 2005


On Mon, 9 May 2005, Servando Garcia wrote:

> 	As you can guess I am working on the riddles. I have looked in vain 
> for a simple example and or a explanation of the string function 
> "translate" or even "maketrans"
> 	 Can someone please send me a working example,please.

Here's a quick example, in which every (lower-case) vowel in a string is
replaced by its vowel position; e.g, 'a' gets replaced by '1', 'e' by
'2', etc., and all consonants (including 'y', for this example) and other
characters are left alone:

>>> from string import maketrans
>>> intab="aeiou"
>>> outtab="12345"
>>> trantab=maketrans(intab, outtab)
>>> instring = "Life of Brian"
>>> outstring = instring.translate(trantab)
>>> outstring
'L3f2 4f Br31n'
>>>






More information about the Tutor mailing list