Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python maketrans() method

Python maketrans() method

Python Strings Python Strings


Description

The Python maketrans() method creates a translation table for character mapping. In its simplest form, which takes two arguments, the first is a string representing the characters to convert, and the second is also a string representing the conversion targets.

**Note:** The two strings must be of the same length, forming a one-to-one correspondence.

Syntax

Syntax of the maketrans() method:



str.maketrans(intab, outtab)

Parameters

Return Value

Returns the new string generated after the translation.

Example

The following example demonstrates using the maketrans() method to convert all vowels into specified digits:

Example

#!/usr/bin/python

-- coding: UTF-8 --

from

string

import

maketrans  

The maketrans function must be called.

intab

=

"aeiou"

outtab

=

"12345"

trantab

=

maketrans

(

intab

,

outtab

)

str

=

"this is string example....wow!!!"

;

print

str

.

translate

(

trantab

)

;

The output of the above example is:



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


Python Strings Python Strings