-5
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??

  • 2
    `replace()` returns a copy of the string. Please read the doc: https://docs.python.org/3/library/stdtypes.html#str.replace – bzu Aug 28 '22 at 11:31

3 Answers3

1

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)
kaksi
  • 88
  • 6
1

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)
GingerCRO
  • 26
  • 4
0
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)
mahieyin-rahmun
  • 1,486
  • 1
  • 12
  • 20