Python Translate and Maketrans Examples

Use the translate method. Create a translation table with maketrans and test performance.

Translate. A translation table maps characters to other characters. In Python we use the translate method to make many character replacements in strings. We build a translation dictionary with maketrans and pass this to translate.Strings

Example. This example first uses str.maketrans. The maketrans method receives two arguments. The first is the "before" and the second is the "after." 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.

Info: Internally translate replaces all the "before" characters to "after" characters. We print some of the variables.

Tip: The table used by translate stores integers. The letter "a" is mapped to 97. This is the ASCII value of "a."

Python program that uses maketrans, translate # 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) Output {97: 100, 98: 101, 99: 102} ddeeff

Ignore, remove. Translate ignores characters that are not specified in the translation table. And keys in the Dictionary with values of None are removed. With maketrans, we specify the "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.

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

Rot13. Rot13 is a cipher: it rotates characters forward 13 places. This makes text unreadable. But as a cipher, it is reversible by applying the translation a second time. We write rot13 with maketrans and translate.ROT13

Note: Due to sheer, crushing boredom I benchmarked a loop-based rot13 against this translate version.

And: My boredom was alleviated when I found that translate is efficient. Please see the next section.

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

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. You can see the loop version on the rot13 page.

This benchmark is poor. The loop version of the code has not been optimized. And only a short string ("gandalf") was converted using rot13. 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.

Benchmark results: Python 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. Please be aware you cannot map characters (like "a"). You must use the integer representation (like 97).

Ord: The ord built-in converts a character to an integer. You can use ord to construct the dictionary keys and values.

None: Also, please use the value None to specify a character that should be removed. This is a special value in Python.


Summary. Machine translation is a field in artificial intelligence. It can become staggeringly complex. The translate method in Python is instead straightforward. It translates characters to other characters.

Review: We used translate with characters. We removed characters and left others unchanged. And we benchmarked translate.

Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to