A New Kind Of String Manipulation
We'll cover the following...
Python strings have many methods. You learned about some of those methods in the Strings chapter: lower()
, count()
, and format()
. Now I want to introduce you to a powerful but little-known string manipulation technique: the translate()
method.
Press + to interact
translation_table = {ord('A'): ord('O')} #①print (translation_table) #②#{65: 79}print ('MARK'.translate(translation_table)) #③#MORK
① String translation starts with a translation table, which is just a dictionary that maps one character to another. Actually, ...