How to find multiple character and replace multiple characters ... | python replace multiple characters
Useatranslationtable[1]:translation_table=str.maketrans({u:w,s:5,i:1})string="us/i5/v"print(string.translate(translation_table))#w5/15/vNotethatrepetivelyusingreplacelikestring.replace("u","w").replace("s","5")...canleadtounwantedresults,ifoneofthereplacementsgetsreplacedlater.Forexample,ifyouwanttoreplaceabybandbbyc,ab.replace(a,b).replace(b,c)#Dont!wouldgivecc,notbc.So,unlessyouresureyouwontgetintosuchproblems,usingstr.translateisabettersolution.Referencestranslationtable(docs.python.org)
Use a translation table[1]:
translation_table = str.maketrans({u: w, s: 5, i:1}) string = "us/i5/v" print(string.translate(translation_table)) # w5/15/vNote that repetively using replace like string.replace("u","w").replace("s","5")... can lead to unwanted results, if one of the replacements gets replaced later. For example, if you want to replace a by b and b by c,
ab.replace(a, b).replace(b, c) # Dont !would give cc, not bc.
So, unless youre sure you wont get into such problems, using str.translate is a better solution.
References translation table (docs.python.org)