python replace() function is not working as supposed

Question:

res = "heeellooo"
D = {"aaa":"a","eee":"e","iii":"i","ooo":"o","uuu":"u","yyy":"y"}
for i,j in D.items():
    res.replace(i,j)
print(res)

the output expected is:
hello
what I got instead is heeellooo

any idea why this is happening??

Asked By: Bounoua Ilyas

||

Answers:

res = "heeellooo"
D = {"aaa":"a","eee":"e","iii":"i","ooo":"o","uuu":"u","yyy":"y"}
for i,j in D.items():
    res = res.replace(i,j) # use the returned value of `replace()`
print(res)
Answered By: mahieyin-rahmun

Classical problem:

res = "heeellooo"
D = {"aaa":"a","eee":"e","iii":"i","ooo":"o","uuu":"u","yyy":"y"}
for i,j in D.items():
    res=res.replace(i,j)
print(res)
Answered By: kaksi

In the for loop you have to mention that certain content of variable "res" needs to be replaced with the certain content from the variable "D".

So, you have to change that line of code to this:

res = res.replace(i,j)
Answered By: GingerCRO