message = "h@ll$ w$rld" # make the replacement ('!'->'a', '@'->'e', etc.) message.translate(str.maketrans("!@#$%", "aeiou")) # outputs hello world
Here is what the above code is Doing:
1. str.maketrans() creates a dictionary that maps each character in the first string to the corresponding character in the second string.
2. The translate() method takes the dictionary created by maketrans() and uses it to replace characters in the string.