Python String maketrans()
Python String maketrans() method
Python String maketrans() method is used to create a mapping table which can be used with the string translate() method. translate() method replaces specific characters in the string based on the mapping table.
In this tutorial, you will learn the syntax and usage of String maketrans() method in Python language.
Syntax of maketrans() method
The syntax of String maketrans() method in Python is given below.
str.maketrans(x, y, z)
Parameters
The string maketrans() method can take three parameters.
Parameter | Description |
---|---|
x | Required If only one argument is specified, then x should be a dictionary of search and replacement characters specified in ascii or unicode format. If two or three arguments are specified, then x should be a string specifying characters in the original string that you would like to replace. |
y | Optional A string value with length equal to that of x. This argument specifies the new replacement characters for that of in x. |
z | Optional A string value. The characters specified in this string shall be removed from the original string. |
Return value
The string lstrip() method returns a String value.
Examples
In the following examples, you will learn how to use maketrans() method with translate() method to replace specific characters with their respective replacements.
1. Using maketrans() to replace specific characters in a string in Python
In this example, we take a string in x. We have to use maketrans() and translate() methods to replace the character l with p, and o with m.
Steps
- Given a string in x.
- Define a mapping table created using maketrans() method that replaces l with p, and o with m.
table = str.maketrans('lo', 'pm')
- Call translate() method on the string x and pass the table as argument to the method.
x.translate(table)
All the characters specified in the first argument to maketrans() method, shall be replaced with the respective replacement characters given in the second argument to maketrans() method. And the resulting string is returned.
- Store the returned string in a variable, say x_translated, and you may print this value.
x_translated = x.translate(table)
Program
The complete program to use maketrans() method to replace a specific character with another.
Python Program
# Given string
x = "hello world"
# Define a mapping table
table = str.maketrans('lo', 'pm')
print(f"Mapping table : {table}")
# Translate given string
x_translated = x.translate(table)
print(f"Original : \"{x}\"")
print(f"Translated : \"{x_translated}\"")
Output
Original : "hello world"
Translated : "heppm wmrpd"
2. Return value of maketrans() method in Python
In this section, we shall create mapping tables using maketrans() with different kinds of inputs, and print the mapping table to output.
maketrans() with one argument
In the following program, we shall create a mapping table by giving a dictionary for argument x of maketrans() method, and print the returned mapping table to output.
Python Program
table = str.maketrans({108: 112, 111: 109})
print(f"Mapping table : {table}")
Output
Mapping table : {108: 112, 111: 109}
maketrans() with two arguments
In the following program, we shall create a mapping table with one character for argument x, and one character for argument y of maketrans() method, and print the returned mapping table to output.
Python Program
table = str.maketrans('l', 'p')
print(f"Mapping table : {table}")
Output
Mapping table : {108: 112}
108 is the ASCII value of character 'l', and 112 is the ASCII value of character 'p'.
In the following program, we shall create a mapping table with two characters for argument x, and two characters for argument y of maketrans() method, and print the returned mapping table to output.
Python Program
table = str.maketrans('lo', 'pm')
print(f"Mapping table : {table}")
Output
Mapping table : {108: 112, 111: 109}
maketrans() with three arguments
In the following program, we shall create a mapping table with two characters for argument x, two characters for argument y, and one character for argument z of maketrans() method, and print the mapping table to output.
Python Program
table = str.maketrans('lo', 'pm', 'w')
print(f"Mapping table : {table}")
Output
Mapping table : {108: 112, 111: 109, 119: None}
Summary
In this tutorial of Python String Methods, we learned about String maketrans() method, its syntax, and examples.