Using replace() method in python by index

The problem is that .replace(old, new) returns a copy of the string in which the occurrences of old have been replaced with new.

Instead, you can swap the character at index i using:

new_str = old_str[:i] + "b" + old_str[i+1:]

Leave a Comment