Python String translate()
Python String translate() method
Python String translate() method returns a string where some specific characters are replaced with replacement characters. These specific characters and their replacements can be described in a dictionary, or in a mapping table.
Consider that given string is in x.
x = "hello world"
Then the return value of x.translate({108: 112}) is
"heppo worpd"
In this tutorial, you will learn the syntax and usage of String translate() method in Python language.
Syntax of translate() method
The syntax of String translate() method in Python is given below.
str.translate(table)
Parameters
The string translate() method takes a single parameter.
Parameter | Description |
---|---|
table | Required A dictionary of ascii-ascii pairs, or a mapping table created using string maketrans() method. These specified mappings are used to replace specified characters with their replacements. |
The difference between using a dictionary and maketrans() is that, using dictionary you can only replace characters but not substrings, whereas with maketrans() you can replace at character level or substring level.
Return value
The string translate() method returns a String value.
Examples
In the following examples, you will learn how to use translate() method to replace specific characters with their respective replacements. First we shall see how to use a dictionary as a table for the translate() method, and then in second example, we shall see how to use a mapping table created from maketrans() method with the translate() method.
1. Using dictionary for table to translate a string in Python
In this example, we take a string in x. We have to translate this string: replace character l with p, and character o with m, by using a dictionary with these mappings, as argument to the translate() method.
Steps
- Given a string in x.
- Define a dictionary with the ascii of l and p as key and value respectively. Similarly, the second entry in this dictionary is the ascii values of o and m as key and value respectively.
table = {108: 112, 111: 109}
# 108 is ascii of l
# 112 is ascii of p
# 111 is ascii of o
# 109 is ascii of m
- Call translate() method on the string x and pass the table as argument to the method.
x.translate(table)
All the specified characters (via keys in dictionary) shall be replaced with the replacement characters (values in dictionary) 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 translate given string with specified replacements.
Python Program
# Given string
x = "hello world"
# Define a mapping table
table = {108: 112, 111: 109}
# Translate given string
x_translated = x.translate(table)
print(f"Original : \"{x}\"")
print(f"Translated : \"{x_translated}\"")
Output
Original : "hello world"
Translated : "heppm wmrpd"
All the occurrences of character l are replaced with p, and all the occurrences of character o are replaced with m.
2. Using mapping table from maketrans() to translate a string in Python
In this example, we define take a string in x. We have to translate this string: replace character l with p, and character o with m, by using a mapping table created from maketrans() method.
Steps
- Given a string in x.
- Define a mapping table created using maketrans() method that replaces l with p and o with m respectively.
table = x.maketrans('lo', 'pm')
- Call translate() method on the string x and pass the table as argument to the method.
x.translate(table)
All the specified characters in the first argument to maketrans() method, shall be replaced with the respective replacement characters given via second argument to the 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 translate given string with specified replacements using table created from maketrans() method.
Python Program
# Given string
x = "hello world"
# Define a mapping table
table = x.maketrans('lo', 'pm')
# Translate given string
x_translated = x.translate(table)
print(f"Original : \"{x}\"")
print(f"Translated : \"{x_translated}\"")
Output
Original : "hello world"
Translated : "heppm wmrpd"
Summary
In this tutorial of Python String Methods, we learned about String translate() method, its syntax, and examples.