I already asked a question exactly like this, but the answer I got still worked and it used the .translate method within the function, but I want to use "for loops" to get the output I am looking for. Here is the link to the questions I asked a while ago: How can I replace multiple characters in a string using python?
I am having some trouble figuring out how to replace multiple characters in a string using a for loop. I am trying to write a functions called replace(string) that takes in an input, and replaces certain letters in the input with another letter.
Lets say I have the string "WXYZ" and I want to replace all the W with Y, X with Z, Y with W, and Z with X. I want it to do the replacement no matter what the input is. So if I also do something like replace("WWZXWWXYYZWYYY") it should replace the letters like I said above.
This is what I have done so far with the for loops:
def replace(string):
for letters in string:
string = string.replace("W","Y").replace("X","Z").replace("Y","W").replace("Z","X")
print(string)
but when I run it with replace("WXYZ")
I get the output of the code as: WXWX
Instead of getting YZWX as the output. I also want to use the built in functions of python as well.
How can I achieve this?