How to use a Translation Table to Replace Characters in Python

Python provides the ability to create Translation Tables.

our_text = "This is an example of some text" translation_table = str.maketrans("abefilostz", "4636110572") print(our_text) #This is an example of some text print(our_text.translate(translation_table)) # Th15 15 4n 3x4mp13 06 50m3 73x7
Code language: Python (python)

First we create a Translation Table by calling str.maketrans(x, y, z), passing in our characters we want to translate from and to.

Then we apply our translate(table) to a string using our newly created Translation Table.

Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments