Python String translate()

Filed Under: Python

Python String translate() function returns a new string with each character in the string replaced using the given translation table.

Python String translate()

The translation table must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None.

We can create a translation table using maketrans() function or provide it manually using a dictionary mapping.

We can pass maximum three string arguments to maketrans() function.

  • If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None.
  • If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y.
  • If there is a third argument, it must be a string, whose characters will be mapped to None in the result.

Python String translate() Examples

Let’s look at some examples of using string translate() function.

maketrans() with one argument


s = 'ABCDBCA'

translation = s.maketrans({ord('A'): 'a', ord('B'): ord('b')})  # single argument as dict
print(s.translate(translation))

Output: abCDbCa

Here ‘A’ is being replaced with ‘a’ and ‘B is being replaced with ‘b’ in the result string.

maketrans() with two arguments


s = 'ABCDBCA'

translation = s.maketrans('A', 'a')

print(s.translate(translation))

translation = s.maketrans('ABCD', 'abcd')
print(s.translate(translation))

Output:


aBCDBCa
abcdbca

The first translation is replacing ‘A’ with ‘a’.

The second translation has two string arguments of the same length and each character in the first string is being mapped with the corresponding index character in the second string. So A will be replaced with a, B will be replaced with b, C will be replaced with c and D will be replaced with d in the result string.

If the two argument strings are of different length, then an error will be raised.


translation = s.maketrans('AB', 'a')

Error: ValueError: the first two maketrans arguments must have equal length

maketrans() with three arguments


s = 'ABCDBCA'

translation = s.maketrans('AB', 'ab', 'ACD')
print(s.translate(translation))

Output: bb

Here ‘A’ is first being replaced by ‘a’ but then overridden to None because of third-string argument. Then ‘B’ is mapped with ‘b’. ‘C’ and ‘D’ characters are mapped to None for translation.

If we provide more than three arguments, then an error is raised.


translation = s.maketrans('AB', 'ab', 'CD', 'c')

Error: TypeError: maketrans() takes at most 3 arguments (4 given)

Python String translate() with manual mapping


s = 'ABCDBCA'

print(s.translate({ord('A'): ord('a'), ord('B'): ord('b'), ord('C'): None}))
print(s.translate({ord('A'): 'X', ord('B'): 'YZ', ord('C'): None}))

Output:


abDba
XYZDYZX

Notice that in the second statement ‘B’ is being replaced by the string ‘YZ’. Other mappings are a simple character to character replacement. I am using ord() function to provide the Unicode code point for the translation mappings.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages