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
๐ŸŒ
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:]).
Discussions

Python: String replace index - Stack Overflow
Using 'string.replace' converts every occurrence of the given text to the text you input. You are not wanting to do this, you just want to replace the text based on its position (index), not based on its contents. More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 29, 2017
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
๐ŸŒ
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)
๐ŸŒ
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....
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-multiple-indices-replace-in-string
Multiple Indices Replace in String โ€“ Python | GeeksforGeeks
January 17, 2025 - String is sliced at each specified index i and the character at that index is replaced with ch by concatenating the parts before and after the index.
๐ŸŒ
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.
๐ŸŒ
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 - string = 'Python' cr = {1: 'X', 3: 'Y', 5: 'Z'} res = '' # Replace multiple characters with different replacement characters for index, replacement in cr.items(): string = string[:index] + cr[index] + string[index + 1:] print(string)
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-ndash-multiple-indices-replace-in-string
Python โ€“ Multiple Indices Replace in String
September 1, 2023 - Define the string and index to replace. Convert a string to a list of characters. Iterate over the index and change the appropriate character. Merge changed characters into a new string. Print a new string. string = "Hello World! Hello Python!" indices = [0, 6, 13] characters = list(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 convert the string into a list and then replace the old character at the particular index with a new character. After the replacement, we can convert the new list into the string using the list.join() method to form a result string again.
๐ŸŒ
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 string concatenation 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....
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python string replace() | a detailed explanation!
Python String Replace() | A Detailed Explanation!
February 21, 2025 - No, you cannot replace a character in a string by directly accessing its index and assigning a new value. This is because strings in Python are immutable, which means their individual characters cannot be modified after the string is created.