Description
Python maketrans () method is used to create character mappings conversion table, for taking two parameters, the easiest way is called, the first argument is a string that represents the character to be converted, the second argument is a string indicating that the conversion goal.
NOTE: The two strings must be the same length as a one to one relationship.
Syntax
maketrans () method syntax:
str.maketrans (intab, outtab)
parameter • intab - string consisting of the string to replace characters. Character string corresponding mapping - outtab; &bull.
The return value
new string Returns string conversion after generation.
Examples
The following example demonstrates the use of maketrans () method to convert all vowels specified number:
#! / Usr / bin / python
# - * - coding: UTF-8 - * -
from string import maketrans # maketrans function must be called.
intab = "aeiou"
outtab = "12345"
trantab = maketrans (intab, outtab)
str = "this is string example .... wow !!!"
print str.translate (trantab)
The above examples output results are as follows: th3s 3s str3ng 2x1mpl2 .... w4w !!! |