I have this string -
test_str = 'hello_2Testsmthhello_1smthhello_1'
and I have a list of strings -
list1 = ['hello_1', 'hello_2']
Now I want to replace hello_1 and hello_2 with a period(.). This is what I've tried -
for i in list1:
h = test_str.replace(str(i), ".")
#print (h)
This gives me the string - .Testsmthhello_1smthhello_1
However, the expected output is - .Testsmth.smth.
Can anyone help me here?
h = test_str.replace(str(i), "."), do you expect this to change the value oftest_str? Why or why not? If it doesn't change, then can you explain how you expect the loop to work?h = test_str.replace(str(i), "."), sohends up with the result of doing the first replacement, andtest_stris the same as before. Right? So, what do you think will happen the second time through the loop, given thattest_strdidn't change? Again: try to think about your code first, and step through the logic of it.