Python String translate() Method
“Strings” are a common data type in Python, and they can be used for text handling, data analysis, etc. Python provides several methods for string manipulation. One such method for string manipulation is the “translate()” method. This method replaces or removes certain characters in a string according to a mapping table.
This write-up presents an in-depth guide on Python “string.translate()” method using the below content:
- What is the “translate()” Method in Python?
- Replacing a Specific Value Using the “translate()” Method.
- Replacing Multiple Values Using the “translate()” Method.
- Mapping Value to None Using the “translate()” Method.
What is the “string.translate()” Method in Python?
Python’s “string.translate()” method takes the translation table and retrieves a string whose character values are mapped to their associated characters. The “maketrans()” method, however, is utilized to create/make a translation table.
Syntax
Parameter
In the above syntax, the “table” parameter can be a dictionary or a mapping table, specifying how the replacement will be done.
Return Value
The “string.translate()” retrieves a string where each character/letter is mapped to its corresponding character according to the translation table.
Example 1: Replacing a Specific Value Using the “string.translate()” Method
The below code is used to replace a specific value by utilizing the mapping table:
print(string_a.translate(str.maketrans("P", "T")))
In the above code:
- The “maketrans()” method is used to create a mapping table that describes the replacement process i.e., replacement character.
- The “translate()” method takes the mapping table and returns the string by mapping each character to the specified character.
Output
As seen, the target character has been replaced successfully with the specified one.
Example 2: Replacing Multiple Values Using the “string.translate()” Method
The following code is used to replace multiple values from the Python string using the discussed method:
map_table = string_a.maketrans({'t':'2','o':'7','i':'3','h':'4'})
print(string_a.translate(map_table))
According to the above code:
- The “maketrans()” method accepts the dictionary and creates a mapping table.
- The “translate()” method accepts the mapping table and retrieves the string by replacing all the specified characters with the corresponding character specified in the dictionary.
Output
The multiple characters in the given string have been replaced with their corresponding allocated values.
Example 3: Mapping Value to None Using the “string.translate()” Method
Let’s overview the following code:
map_table = string_a.maketrans('P', 'C', '%#$')
print(string_a.translate(map_table))
In the above example code:
- The “maketrans()” method takes three arguments and creates a mapping table.
- The first argument character is replaced with the second argument character, while the third argument value is mapped to none.
- The “translate()” method takes the mapping table as an argument and translates the string based on the translation table.
Output
The specified character has been replaced and mapped to none.
Conclusion
The “string.translate()” method in Python takes the translation table and returns a string mapped to the character values. The translation table is created via the “string.maketrans()” method. The “string.translate()” method maps both the single or multiple-character values with the specified character by accepting the translation table or dictionary. This write-up presented a thorough guide on Python’s “string.translate()” method using numerous examples.
Source: linuxhint.com