python string replace index

# strings are immutable in Python, 
# we have to create a new string which 
# includes the value at the desired index

s = s[:index] + newstring + s[index + 1:]

Here is what the above code is Doing:
1. We’re creating a new string that starts with the first character of the old string, up to the character at index.
2. We’re adding the new string to the end of the first part.
3. We’re adding the rest of the old string to the end of the new string.

This is a very common pattern in Python, so it’s worth knowing.