Python - String Method - translate() Tutorial
This method will return a string where some specified characters described in a dictionary or in mapping table are replaced with the corresponding character.
Syntax-
String.translate(x)
#x - It can be either dictionary or mapping table describing how to perform replace
Example-
str1 = "Hello Sam"
#using a dictionary with ascii codes to replace 97 (a) with 65 (A):
mydict1 = {97: 65}
print(str1.translate(mydict1))
#using a dictionary with ascii codes to remove some character by specifying None
mydict2 = {83: None, 108: None, 111: 121}
print(str1.translate(mydict2))
Output-
Hello SAm
Hey am