str.maketrans()
str.maketrans(x[, y[, z]]) dict · Added in v3.1 · Updated March 13, 2026 · String Methods The maketrans() method creates a translation table that can be used with the translate() method. It is useful when you need to replace or remove specific characters from a string.
Syntax
str.maketrans(x[, y[, z]])
Parameters
The method can be called in three different ways:
Single argument (dictionary)
- x: A dictionary that maps Unicode ordinals (integers) or characters to Unicode ordinals, strings, or None
Two arguments (two strings)
- x: A string of characters to be replaced
- y: A string of replacement characters (must be same length as x)
Three arguments (two strings + one string)
- x: A string of characters to be replaced
- y: A string of replacement characters (must be same length as x)
- z: A string of characters to be deleted
Returns: A dictionary representing the translation table.
Examples
Basic character replacement with two arguments
# Create a translation table
table = str.maketrans("aeiou", "AEIOU")
# Apply it to a string
text = "hello world"
print(text.translate(table))
# hEllO wOrld
The first string specifies characters to replace, and the second specifies their replacements. Positions are matched one-to-one.
Using a dictionary
# Map specific characters using a dictionary
table = str.maketrans({
ord('a'): ord('x'),
ord('e'): ord('y'),
ord('i'): ord('z')
})
text = "aeiou"
print(text.translate(table))
# xyzou
This approach gives you precise control over which characters get translated to what.
Deleting characters with three arguments
# Delete vowels from the string
table = str.maketrans("aeiou", "aeiou", "aeiou")
text = "hello world"
print(text.translate(table))
# hll wrld
When three arguments are provided, the third string specifies characters to delete entirely.
Practical example: sanitizing input
# Remove non-alphanumeric characters except spaces
special_chars = "!@#$%^&*()_+-=[]{}|;':\",./<>?"
table = str.maketrans("", "", special_chars)
text = "hello@world#2024!"
print(text.translate(table))
# helloworld2024
This is useful for cleaning user input or normalizing strings.
Converting case with maketrans
# Swap uppercase for lowercase and vice versa
import string
table = str.maketrans(string.ascii_uppercase, string.ascii_lowercase)
text = "HELLO World"
print(text.translate(table))
# hello world
Common Patterns
Password sanitization
# Remove special characters from a potential password
special_chars = "!@#$%^&*()_+-=[]{}|;':\",./<>?"
table = str.maketrans("", "", special_chars)
password = "p@ssw0rd!123"
safe = password.translate(table)
print(safe)
# password123
Normalizing text
# Replace various dash characters with regular hyphen
dashes = "–—−" # en-dash, em-dash, minus sign
table = str.maketrans(dashes, "---")
text = "Hello – World — is − here"
print(text.translate(table))
# Hello - World - is - here
Return Value
maketrans() returns a dictionary that maps Unicode ordinals to either:
- Another ordinal (for replacement)
- A string (for replacement)
- None (for deletion)
This dictionary is then passed to the translate() method.