Latest web development tutorials

Python3 maketrans () method

Python3 string Python3 string


description

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 parameter is the string representation of goal conversions .

Note: The two strings must be the same length as a one to one relationship.

grammar

maketrans () method syntax:

str.maketrans(intab, outtab)

parameter

  • intab - the string to replace the string of characters.
  • outtab - string corresponding mapping characters.

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/python3

intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)

str = "this is string example....wow!!!"
print (str.translate(trantab))

Examples of the above output results are as follows:

th3s 3s str3ng 2x1mpl2....w4w!!!

Python3 string Python3 string