Need help on replace.()!thx!

I’m playing around with the REPLACE.() method.
How do I replace all occurrences of an argument with a certain range of numbers?

I created a long string and counted ‘abc’ has 12 occurrences. I wanted to replace them with numbers 1- 12
So I tried to use a for loop but failed.

Is there anything wrong with my code? Is this even achievable by using replace.()?
or is there any other way to get the result I want.

THXXXXXXX!!! XOXO!!!

hi @glamooranna ,

Unfortunately, the moment your .replace method get executed, it changes all occurrences at once.

Also, your ‘a’ variable long string doesn’t get replace as it wasn’t stored up. Hence the variable value remain the same.

The following is the example I tried;

# String Sample a = "12345abc12345abc12345abc12345" # String value to replace rep = 'abc' # No. of occurrence print(a.count(rep)) # Replace string value with 'G' c = a.replace(rep, 'G') # Output print(c)

What I could suggest you is to lookup on the method again because there’s one more parameter you left out in order to make it work.

I’ve not tried your code but it looks like replace as you’ve used it will replace ‘all’ occurances of ‘abc’ on the first loop.
It has a an optional third parameter for count (which will only replace ‘abc’ ‘count’ times for each loop.)

So, if you alter it to;
a.replace(‘abc’, str(x), 1 )
it should just replace the first instance on each pass through the loop.

Edit:
also remember that the range of the loop begins at 0, so you may need to edit the str(x) to compensate