Translation Tables

Harold Nelson

10/20/2020

Build a Translation Table


intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)

Use the table.

s = "This is a string with vowels"
s2 = s.translate(trantab)
s2
## 'Th3s 3s 1 str3ng w3th v4w2ls'

What happened?

All the lower case values were replaced with the numerical values according to the table transtab.

Look at trantab.

trantab
## {97: 49, 101: 50, 105: 51, 111: 52, 117: 53}

What is trantab?

It’s a dictionary.

the keys are the numbers of characters potentially present in the string to be translated.

The values are the numbers of the characters with which the corresponding keys will be replaced.