How to use a Translation Table to Replace Characters in Python

0 min read 74 words

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

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:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags