str.translate()

str.translate(table)
Returns: str · Updated March 13, 2026 · String Methods
strings translation character-mapping

The .translate() method returns a copy of the string where each character has been mapped through a translation table. It’s commonly used for character-level replacements, deletions, and Unicode normalization. The method works closely with str.maketrans(), which creates the translation table.

Syntax

str.translate(table)

Parameters

ParameterTypeDescription
tabledict or NoneA dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings, or None. If None, delete the mappings.

Returns: A new string with characters mapped according to the table.

Creating Translation Tables

Using str.maketrans()

The str.maketrans() function creates a translation table for use with .translate():

>>> table = str.maketrans({'a': 'x', 'e': 'y'})
>>> "hello world".translate(table)
'hyllo world'

With two arguments (character-to-character mapping)

>>> table = str.maketrans('aeiou', '12345')
>>> "hello world".translate(table)
'h2ll4 w4rld'

With optional third argument (characters to delete)

>>> table = str.maketrans('aeiou', '12345', 'l')
>>> "hello world".translate(table)
'he44 w4rd'

Examples

Basic character replacement

>>> table = {'a': 'z', 'b': 'y'}
>>> "abc".translate(table)
'zyc'

Deleting characters

>>> "hello world".translate({ord(c): None for c in 'aeiou'})
'hll wrld'

Or using the third argument to maketrans():

>>> table = str.maketrans('', '', 'aeiou')
>>> "hello world".translate(table)
'hll wrld'

Unicode character translation

>>> table = {ord('é'): ord('e'), ord('ü'): ord('u')}
>>> "café résumé".translate(table)
'cafe resume'

Practical: cleaning user input

# Remove all non-ASCII characters
>>> table = str.maketrans('', '', ''.join(chr(i) for i in range(128, 256)))
>>> "hello café world 😀".translate(table)
'hello cfe world '

Converting to ASCII-compatible text

# Replace non-ASCII with closest ASCII equivalents
trans_table = str.maketrans({
    'à': 'a', 'á': 'a', 'â': 'a',
    'é': 'e', 'è': 'e', 'ê': 'e',
    'ñ': 'n', 'ü': 'u', 'ö': 'o'
})
>>> "café résumé naïve".translate(trans_table)
'cafe resume naive'

Common Patterns

Removing punctuation

>>> import string
>>> table = str.maketrans('', '', string.punctuation)
>>> "Hello, world!".translate(table)
'Helloworld'

Normalizing whitespace

# Replace multiple spaces with single space
>>> "hello    world".translate(str.maketrans('', '', '\t\n\r\x0b\x0c'))
'hello world'

Character case conversion with delete

>>> table = str.maketrans('', '', string.ascii_lowercase)
>>> "Hello123".translate(table)
'H123'

Differences from replace()

  • .translate() works at the character level using a mapping table
  • .replace() works with string substitution patterns
  • .translate() is faster for bulk character operations
  • .translate() can delete characters (by mapping to None)
# Using replace() - word by word
>>> "hello hello".replace("l", "x")
'hexxo hexxo'

# Using translate() - character by character
>>> "hello hello".translate(str.maketrans('l', 'x'))
'hexxo hexxo'

Errors

  • If table is not a dictionary or None, TypeError is raised
>>> "hello".translate("invalid")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: translation table must be dict, None, or a callable

See Also