

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python maketrans() and translate() functions
Python String maketrans()
The maketrans() method returns a mapping table for translation usable for translate() method. This is a static method that creates a one to one mapping of a character to its translation/replacement. This method creates a Unicode representation of each character for translation.
The syntax of maketrans() method is −
string.maketrans(x[, y[, z]]) y and z are optional arguments.
String maketrans() Parameters
The maketrans() method has 3 parameters.
x - In this case if only one argument is supplied, it must be a dictionary and the dictionary should contain 1-to-1 mapping from a single character string to its translation OR a Unicode number (97 for 'a') to its translation.
y - In this case if two arguments are passed, it must be two strings with equal length and each character in the first string is a replacement to its corresponding index in the second string.
z - In this case if three arguments are passed, each character in the third argument is mapped to none.
Return value from String maketrans()
This method returns a translation table with a 1-to-1 mapping of a Unicode ordinal to its translation/replacement.
Example1
Translation table using a dictionary with maketrans()
Example code
dict= {"a": "123", "b": "456", "c": "789"} my_string = "abc" print(my_string.maketrans(dict)) # example dictionary dict = {97: "123", 98: "456", 99: "789"} my_string = "abc" print(my_string.maketrans(dict))
Output
{97: '123', 98: '456', 99: '789'} {97: '123', 98: '456', 99: '789'}
Explanation − In this example, dict is a dictionary and It contains a mapping of characters a,b and c to 123, 456 and 789 respectively. The maketrans() creates a mapping of the character's Unicode ordinal to its corresponding translation. So, the result is 97 ('a') is mapped to '123', 98 'b' to 456 and 99 'c' to 789.
Example2
Translation table using two strings with maketrans()
Example
my_string1 = "abc" my_string2 = "def" string = "abc" print(string.maketrans(my_string1, my_string2)) # Example dictionary my_string1 = "abc" my_string2 = "defghi" string = "abc" print(string.maketrans(my_string1, my_string2))
Output
{97: 100, 98: 101, 99: 102} ValueError: the first two maketrans arguments must have equal length
Explanation − In this example two strings of equal length "abc" and "def" are defined. And the corresponding translation is created and printing only the first translation gives you a 1-to-1 mapping to each character's Unicode ordinal in my_string1 to the same indexed character on the my_string2.
But in this case, 97 ('a') is mapped to 100 ('d'), 98 ('b') to 101 ('e') and 99 ('c') to 102 ('f') and trying to create a translation table for unequal length strings raises a ValueError exception indicating that the strings must have equal length.
translate(). We know that a translation table maps characters to other characters. We use the translate() method in Python for making many character replacements in strings. First we build a translation dictionary with maketrans() and pass this to translate.
Python program that uses maketrans, translate
#Python program that uses maketrans, translate dict = str.maketrans("abc", "def") print(dict) # Translate this value. value = "aabbcc" result = value.translate(dict) print(result)
Output
{97: 100, 98: 101, 99: 102} ddeeff
Python program that ignores, removes characters
# Create translation table. table = str.maketrans("78", "12", "9") # Translate this string. input = "123456789" result = input.translate(table) # Write results. print(input) print(result)
Output
123456789 12345612
Python program that applies rot13 translation
#Create translation table. trans = str.maketrans("abcdefghijklmnopqrstuvwxyz","nopqrstuvwxyzabcdefghijklm") # Apply rot13 translation. print("gandalf".translate(trans)) print("gandalf".translate(trans).translate(trans))
Output
tnaqnys gandalf
- Related Questions & Answers
- How we can translate Python dictionary into C++?
- The CSS translate() Function
- Mathematical Functions in Python - Special Functions and Constants
- Python startswith() and endswidth() functions
- How to translate a python string according a given dictionary?
- Why and how are Python functions hashable?
- Write two functions isPrime and primeFactors (Python)
- Python Mathematical Functions
- Are Python functions objects?
- Statistical Functions in Python
- Decimal Functions in Python
- Operator Functions in Python
- Partial Functions in Python?
- Time Functions in Python?
- Mathematical Functions in Python?