Home
Python
String translate Examples
Updated Feb 18, 2023
Dot Net Perls
Translate. A translation table maps characters to other characters. In Python we use the translate method to make many character replacements in strings.
Some notes. We build a translation dictionary with maketrans and pass this to translate. This approach can be reused in many Python programs.
Example. This example first uses str.maketrans. The maketrans method receives 2 arguments. The first is the before and the second is the after.
Info The characters in the first are mapped to the same-positioned characters in the second.
Then We call the translate method. We pass the dictionary argument returned by str.maketrans.
Important Internally translate replaces all the before characters to after characters. We print some of the variables.
# Make a translation table. # ... Map a to d, b to e, and c to f. dict = str.maketrans("abc", "def") print(dict) # Translate this value. value = "aabbcc" result = value.translate(dict) print(result)
{97: 100, 98: 101, 99: 102} ddeeff
Ignore, remove. Translate ignores characters that are not specified in the table. Keys with values of None are removed. With maketrans, we specify "remove" characters as the third argument.
Here We change the characters 7 and 8. We remove the character 9. And the translate method ignores all others.
# Create translation table. table = str.maketrans("78", "12", "9") # Translate this string. input = "123456789" result = input.translate(table) # Write results. print(input) print(result)
123456789 12345612
Rot13. Rot13 rotates characters forward 13 places. It is reversible by applying the translation a second time. We write rot13 with maketrans and translate.
ROT13
Note I benchmarked a loop-based rot13 against this translate version. And I found that translate is efficient. Please see the next section.
# Create translation table. trans = str.maketrans("abcdefghijklmnopqrstuvwxyz", "nopqrstuvwxyzabcdefghijklm") # Apply rot13 translation. print("gandalf".translate(trans)) print("gandalf".translate(trans).translate(trans))
tnaqnys gandalf
Performance. Next I tested the performance of translate. I tested the rot13 translation. I compared the version shown on this page (which uses translate) against a loop-based version.
But The test shows that translate is fast. Using translate is faster than the loop-based version.
Thus I advise using translate instead of other approaches, like testing each character in a loop, for most Python requirements.
1385003203.634 1385003203.885 translate method: 0.25 s 1385003205.569 rot13 method: 1.68 s
Maketrans. The maketrans method does not need to be used. You can create a dictionary and store integer keys and values in it. You must use the integer representation (like 97).
Detail The ord built-in converts a character to an integer. You can use ord to construct the dictionary keys and values.
Also Use the value None to specify a character that should be removed. This is a special value in Python.
None
A summary. The translate method in Python is straightforward—it translates characters to other characters. We removed characters and left others unchanged. And we benchmarked translate.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Feb 18, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen