I have an unintelligible string of characters with a simple key: every letter's true value is the letter +2 in the alphabet. Example: A --> C ; M --> O.
I wrote a simple python function that accepts two arguments, a string and a dictionary. It then searches for a character in the string, finds that key in the dictionary, and then returns the key's value. Problem is, I have it looping through and so when it finds an 'a', it replaces it with a 'c', which then replaces that with an 'e', then a 'g', and so on. So at the end, I have a big string of As and Bs.
alphadict = {'a': 'b', 'b': 'c':, 'c': 'd'...}
codestring = "dj afioj asfd sfijiojj ddk weic."
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
newtext = replace_all(codestring, alphadict)
print newtext
Can anyone help me figure out how to generate the function to replace a character once, then move on to the next without looping back through?