1

I want to use string.replace using for loop. This is my code:

new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']

for i in range(0,len(old)):
    my_str = string.replace(my_str,old[i],new[i])

print(my_str)

But it is giving me error:

TypeError: 'str' object cannot be interpreted as an integer

Desired output:

there are two much person p, person q, person r.

This is just an example, I want to run a for loop for 10,000 length list.

6
  • 2
    Can not reproduce. Your code runs fine on Python 2.7 Commented Jul 28, 2016 at 16:45
  • The string.replace function has been deprecated for a very long time. You should use thestr.replace method instead. Commented Jul 28, 2016 at 16:46
  • 4
    You know, even if you fix whatever weird problem you had, you're still going to replace the a in are and the c in much. Commented Jul 28, 2016 at 16:47
  • But it doesnt work for this example, I have written in answer part. Commented Jul 28, 2016 at 16:48
  • 2
    Based on your update, which you've since deleted, the problem is the 'Y' in Years is caps, and in 'years' is lower case. Next time, post a minimal reproducible example. Commented Jul 28, 2016 at 16:56

3 Answers 3

3

Try

new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']

for i in range(len(old)):
    my_str = my_str.replace(old[i],new[i])

print(my_str)

but that is propably not very fast

If entries in old are all letters-only you can do

import re

new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']

word=re.compile(r"\w*") # word characters
old_new=dict(zip(old,new))
ong=old_new.get
my_str=word.sub((lambda s:ong(s,s)),my_str)

print(my_str)

this also avoids the double replacement problem if an entry is in both old and new (not avoided in the shorter solution)

Sign up to request clarification or add additional context in comments.

Comments

2

Actually, I can not reproduce your problem; your code runs fine on Python 2.7. However, there are better ways to do it. First, instead of using a range, you could zip the old and new lists:

for i in range(0,len(old)):
    my_str = string.replace(my_str,old[i],new[i])

However, this will still replace the a in are and the c in much, and it might also replace characters that were introduced in an earlier replacement, which is probably not what you want. Instead, you could use the re module, joining the strings to be replaced with | to a regex and delimiting it with \b word boundary characters, e.g. \b(a|b|c)\b in your case, and use a dictionary to look up the proper replacements.

d = dict(zip(old, new))
p = r'\b(' + '|'.join(old) + r')\b'
my_str = re.sub(p, lambda m: d.get(m.group()), my_str)

Result: there are two much person p, person q, person r.

3 Comments

like my solution this only works if entries in old only consist of word-characters
.join(old) may not be a good idea if len(old) is 10000. OTOH, the OP may have an XY problem...
@PM2Ring Good point, in this light janbrohl's solution might work better. +1
1

string.replace() is available on Python 2.x but deprecated in python 3.x.

Below is how you can use it in Python 3.x

str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

In your case, it is my_str.replace(old[index], new[index])

Comments

Your Answer

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.