0

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?

asked Mar 22, 2021 at 5:33
4
  • Where you have written h = test_str.replace(str(i), "."), do you expect this to change the value of test_str? Why or why not? If it doesn't change, then can you explain how you expect the loop to work? Commented Mar 22, 2021 at 5:37
  • In general, you should try to think about your code first, and step through the logic of it. Please read ericlippert.com/2014/03/05/how-to-debug-small-programs . Commented Mar 22, 2021 at 5:38
  • Okay, so the first time through the loop, you do h = test_str.replace(str(i), "."), so h ends up with the result of doing the first replacement, and test_str is the same as before. Right? So, what do you think will happen the second time through the loop, given that test_str didn't change? Again: try to think about your code first, and step through the logic of it. Commented Mar 22, 2021 at 5:43
  • @KarlKnechtel - Thanks for your advice.. I didn't think of it that way.. Commented Mar 22, 2021 at 5:46

4 Answers 4

1

You can simply change the h in the loop to test_str :)

for i in list1:
 test_str = test_str.replace(str(i), ".")

In your original loop, you did successfully replace the substrings with . in test_str but you didn't save this change. test_str stayed the same and h got a brand new value in each loop. As you can see by printing the values during the loop.

for i in list1:
 print(i)
 h = test_str.replace(str(i), ".")
 print(h)
 print(test_str)

Hopefully I explained this clearly.

answered Mar 22, 2021 at 5:47
0

replace() returns a copy of the string with the replaced version so you need to assign it back to test_str:

test_str = 'hello_2Testsmthhello_1smthhello_1'
list1 = ['hello_1', 'hello_2']
for i in list1:
 test_str = test_str.replace(i, ".")
 
print(test_str) #.Testsmth.smth.
answered Mar 22, 2021 at 5:36
0

It is because test_str is replaced only once. Do not use h for your intent.

for i in list1:
 test_str = test_str.replace(str(i), ".")

If you do not want to change test_str, then make a copy as follows.

import copy
h = copy.copy(test_str)
for i in list1:
 h = h.replace(str(i), ".")
answered Mar 22, 2021 at 5:36
0

You can use regex for it

import re
test_str = 'hello_2Testsmthhello_1smthhello_1'
pattern = r'hello_\d'
print(re.sub(pattern,'.', test_str))
answered Mar 22, 2021 at 5:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.