Latest web development tutorials

Python maketrans () method

Python strings Python strings


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 parameter is the string representation of the conversion aims.

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/python
# -*- coding: UTF-8 -*-

from string import maketrans   # 必须调用 maketrans 函数。

intab = "aeiou"
outtab = "12345"
trantab = 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!!!

Python strings Python strings