Use of the maketrans () method in Python2.x, python2.xmaketrans
The string intab returned by the maketrans () method maps each character to a conversion table with the same position as the character string outtab. The table is then passed to the translate () function.
Note: Both intab and outtab must have the same length.
Syntax
The syntax of the maketrans () method is as follows:
Str. maketrans (intab, outtab]);
Parameters
- Intab -- this is the actual character string.
- Outtab -- this is the string of the corresponding ing characters.
Return Value
This method uses the translate () function to convert the table.
Example
The following example shows how to use the maketrans () method. Here, the position where each vowel in a string replaces its vowel:
#! /Usr/bin/pythonfrom string import maketrans # Required to call maketrans function. intab = "aeiou" outtab = "12345" trantab = maketrans (intab, outtab) str = "this is string example .... wow !!! "; Print str. translate (trantab );
When we run the above program, it will produce the following results:
Th3s 3 s str3ng 2x1mpl2... w4w !!!