HOWTO · Python

The maketrans Function in Python

Learn how Python's str.maketrans() builds character translation tables for str.translate(), including replacements, deletion, and practical text cleanup.

On this page

Python’s str.maketrans() creates a translation table, and str.translate() applies that table to a string. Keeping these jobs separate lets you build a table once and reuse it on multiple strings. The table maps Unicode code points (the integer values of characters), so this works with Unicode text as well as ASCII.

How str.maketrans() and str.translate() work

str.maketrans() is a static method on str; call it as str.maketrans(...). Its result is a mapping that str.translate() looks up for each character in the input string. A mapping value can be another code point or a string, while None means that the character is removed. Characters absent from the table pass through unchanged. Translation operates character by character, so it does not find or replace multi-character substrings.

The table itself does not change text. Call translate() on the string you want to transform and pass the table as its argument. The original string remains unchanged; the method returns a new string.

Create a translation table from a mapping

Pass one mapping to str.maketrans(). Keys are Unicode code points represented by integers or one-character strings; values are replacement code points or strings. For example, the mapping below changes a to 1, b to 2, and c to 3.

Run this example:

table = str.maketrans({"a": "1", "b": "2", "c": "3"})
source = "abcde"
translated = source.translate(table)
print(table)
print(translated)
{97: '1', 98: '2', 99: '3'}
123de

The output shows the mapping table and then the translated string. The table is suitable for reuse with other strings; characters it does not mention stay as they are.

Create a translation table from two strings

The two-string form pairs characters by position: each character in the first string maps to the character at the same position in the second. Both strings must have equal lengths. This is convenient for simple one-character substitutions; use the mapping form when replacements are easier to express as explicit pairs.

table = str.maketrans("abc", "123")
source = "cab"
translated = source.translate(table)
print(translated)
312

Here, a maps to 1, b to 2, and c to 3. Matching is case-sensitive, so an uppercase character is unchanged unless it also has an entry in the table.

Delete characters with the third argument

Pass a third string to specify characters to delete. The first and second strings still define replacements, and every character in the third string is mapped to None. To delete characters without replacements, pass empty strings for the first two arguments.

table = str.maketrans("", "", "aeiou")
source = "Hello, World!"
translated = source.translate(table)
print(translated)
Hll, Wrld!

Each listed lowercase vowel is removed wherever it occurs. Add uppercase vowels separately if those should also be removed. Deletion and replacement can be combined in one table by providing replacement strings and a third deletion string.

Apply translation tables to practical text tasks and understand their limits

Translation tables are useful for predictable, character-level cleanup, such as removing a known set of punctuation marks or converting a fixed set of characters. The following example removes selected punctuation while leaving other characters intact:

table = str.maketrans("", "", "!@#")
username = "User!@Name#123"
cleaned = username.translate(table)
print(cleaned)
UserName123

This is only a character transformation. It does not validate a username, enforce an application’s allowed-character policy, or make untrusted input safe for a database, shell, HTML, or another context. Use context-appropriate validation and escaping for those tasks.

Because translation is per character, it cannot replace a substring such as "cat" as a unit. Use str.replace() for a literal substring replacement. Translation also does not perform Unicode normalization, case folding, or linguistic text conversion; apply the appropriate Unicode or text-processing operation when those behaviors are needed. Use integer code points or one-character mapping keys for character mappings, and ensure the two strings in the positional form have equal lengths.