Python Forum

Full Version: How do you switch all alphabet into different characters?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to switch multiple characters into other characters e.g
m--->n
lo--->om

no never-->mo mever
hello-->helom

i.e .replace() but for multiple characters and character groups like dipthongs
You can store your replacements in a dictionary and then you can do the replacement with simple loop:

replacements = {
    "m": "n",
    "lo": "om",
    "no never": "mo mever",
    "hello": "helom"
}

my_string = "m lo"

for (old, new) in replacements.items():
    my_string = my_string.replace(old, new)

print(my_string)