As strings are immutable in Python, just create a new string which includes the value at the desired index.

Assuming you have a string s, perhaps s = "mystring"

You can quickly (and obviously) replace a portion at a desired index by placing it between "slices" of the original.

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

You can find the middle by dividing your string length by 2 len(s)/2

If you're getting mystery inputs, you should take care to handle indices outside the expected range

def replacer(s, newstring, index, nofail=False):
    # raise an error if index is outside of the string
    if not nofail and index not in range(len(s)):
        raise ValueError("index outside given string")

    # if not erroring, but the index is still not in the correct range..
    if index < 0:  # add it to the beginning
        return newstring + s
    if index > len(s):  # add it to the end
        return s + newstring

    # insert the new string between "slices" of the original
    return s[:index] + newstring + s[index + 1:]

This will work as

replacer("mystring", "12", 4)
'myst12ing'
Answer from ti7 on Stack Overflow
Discussions

How to replace a specific index position in a list
Hello! So, just as the title says, how do I replace a specific index position? If you look at the bottom where it says blankword.replace(blankword[i],guess), that is what I’m having trouble with. I think I know why it doesn’t work, but I don’t know what else to do. More on discuss.python.org
🌐 discuss.python.org
4
0
November 3, 2021
How to replace a string that is all the same character at a specific index?
Slice and concatenate. string1 = string1[:-1] + 'b' More on reddit.com
🌐 r/learnpython
10
10
May 4, 2024
Using replace() method in python by index - Stack Overflow
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: More on stackoverflow.com
🌐 stackoverflow.com
May 28, 2017
Replace element in Nested List.
Here's a very concise version that uses list comprehensions: matrix = [[value if value != choice else 'X' for value in row] for row in matrix] More on reddit.com
🌐 r/learnpython
9
2
September 21, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-replace-to-k-at-ith-index-in-string
Replace a String character at given index in Python - GeeksforGeeks
July 15, 2025 - Slicing is one of the most efficient ways to replace a character at a specific index. ... The string is split into two parts: everything before the target index (text[:index]) and everything after (text[index+1:]).
🌐
Python Examples
pythonexamples.org › python-string-replace-character-at-specific-position
Python - Replace Character at Specific Index in String
Python - Replace character at given index - To replace a character with a given character at a specified index, you can use python string slicing; or convert the string to list, replace and then back to string.
🌐
Python.org
discuss.python.org › python help
How to replace a specific index position in a list - Python Help - Discussions on Python.org
November 3, 2021 - Hello! So, just as the title says, how do I replace a specific index position? If you look at the bottom where it says blankword.replace(blankword[i],guess), that is what I’m having trouble with. I think I know why it do…
🌐
FavTutor
favtutor.com › blogs › replace-character-string-python
Python Replace Character in String | FavTutor
October 6, 2021 - ... str = 'Python' indexes = {2: 'a', 4: 'b', 5: 'c'} result = '' # Replace multiple characters with different replacement characters for index, replacement in indexes.items(): str = str[:index] + indexes[index] + str[index + 1:] print(str)
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-multiple-indices-replace-in-string
Multiple Indices Replace in String - Python - GeeksforGeeks
July 12, 2025 - s = "geeksforgeeks is best" li ... comprehension we check each character's index (idx) in temp, if the index is in li then it replaces the character with ch....
🌐
DEV Community
dev.to › fedingo › how-to-replace-character-at-nth-index-in-python-string-1b7p
How to Replace Character at Nth Index in Python String - DEV Community
May 10, 2024 - In this approach, we use list() function to convert the string into a list. Then we replace the desired character using its index. Then we convert python list back into string using join() function.
🌐
GeeksforGeeks
geeksforgeeks.org › python-multiple-indices-replace-in-string
Multiple Indices Replace in String – Python | GeeksforGeeks
January 17, 2025 - For loop iterates through the indices provided in li and for each index the string is updated by replacing the character at that specific index. ... In Python, replacing multiple lines in a file consists of updating specific contents within a text file. This can be done using various modules ...
🌐
Python Guides
pythonguides.com › replace-a-character-at-a-specific-index-in-a-string-using-python
How To Replace A Character At A Specific Index In A String Using Python?
March 19, 2025 - Python provides a built-in replace() ... of the specified substring. If you only want to replace a character at a specific index, you can combine the replace() method with slicing....
🌐
TutorialsPoint
tutorialspoint.com › python-ndash-multiple-indices-replace-in-string
Python – Multiple Indices Replace in String
September 1, 2023 - This method is more efficient for multiple replacements as it avoids creating intermediate strings. ... text = "Hello World! Hello Python!" indices = [0, 6, 13] # Convert to list of characters characters = list(text) # Replace characters at specified indices for index in indices: characters[index] = '#' # Join back to string new_text = ''.join(characters) print("Modified string:", new_text)
🌐
Delft Stack
delftstack.com › home › howto › python › python replace character in string at index
How to Replace Character in String at Index in Python | Delft Stack
February 2, 2024 - The following code uses string slicing to replace a character in a string at a certain index in Python.
🌐
EyeHunts
tutorial.eyehunts.com › home › python replace character in a string by index | example code
Python replace character in a string by index | Example code - EyeHunts
August 4, 2021 - First, convert the string to a list, then replace the item at the given index with a new character, and then join the list items to the string. string = 'EyeHunte' position = 7 new_character = 's' temp = list(string) temp[position] = new_character ...
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-replace-a-character-at-a-specific-index
Python program to change character of a string using given index
July 11, 2023 - def change_multiple_characters(s, changes): char_list = list(s) for index, char in changes.items(): if 0 <= index < len(char_list): char_list[index] = char return ''.join(char_list) # Example usage s = "python" changes = {1: 'Y', 3: 'P', 5: 'N'} result = change_multiple_characters(s, changes) print(f"Original: {s}") print(f"Modified: {result}") ... Use string slicing for single character replacement and list conversion for multiple changes.
🌐
Dirask
dirask.com › posts › Python-replace-part-of-string-from-given-index-1Godq1
Python - replace part of string from given index
In this example, we use string slicing with join() method to replace CD letters (index 1-3) from the text with the replacement.
🌐
Programiz
programiz.com › python-programming › methods › string › replace
Python String replace()
Python String replace() Python ... Python String find() Python ascii() Python String index() The replace() method replaces each matching occurrence of a substring with another string....
🌐
Scaler
scaler.com › home › topics › replace a character in a string python
Replace a Character in a String Python - Scaler Topics
April 20, 2024 - We can choose the character that has to be replaced by slicing the original string. After slicing, we can easily replace the required character with the new character. We can convert the string into a list and then replace the old character at the particular index with a new character.