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
🌐
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…
Discussions

Using replace() method in python by index - Stack Overflow
You may want to try something like new_str = "b" + example_string[1:] . replace(a,b) is used for swapping all occurrences of string a with b. ... I don't understand, you want to substitute the first 't' character of a string with 'b' ? Or is the index the key to decide what to replace (instead ... More on stackoverflow.com
🌐 stackoverflow.com
May 28, 2017
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
arrays - How to replace values at specific indexes of a python list? - Stack Overflow
@dbr: I think he wants to replace values in s indexed by indexes in l with values from m, so there's no problem with out-of-boundsness here. ... Save this answer. ... Show activity on this post. The biggest problem with your code is that it's unreadable. Python code rule number one, if it's ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Python Examples
pythonexamples.org › python-string-replace-character-at-specific-position
Python - Replace Character at Specific Index in String
In the following example, we replace the character at index=6 with e. string = 'pythonhxamples' position = 6 new_character = 'e' string = string[:position] + new_character + string[position+1:] print(string) Initially, string = 'pythonhxamples'. string[:position]: Extracts the substring 'python'. new_character = 'e': The character to be inserted. string[position+1:]: Extracts the substring 'xamples' starting from index 7. The final string is formed by concatenating these parts: 'python' + 'e' + 'xamples'.
🌐
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 ...
🌐
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 - The pattern matches the character at the desired index using lookbehind. The re.sub() method replaces the matched character with the specified replacement. This method is powerful but less efficient and more complex for simple replacements.
🌐
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. stra = "Meatloaf" posn = 5 nc = "x" stra = string[:posn] + nc + string[posn + 1 :] print(stra) ...
🌐
stataiml
stataiml.com › posts › 40_replace_item_list_python
Python: Replace Value at Specific Index in List and Array - stataiml
May 14, 2024 - # replacing the value at index 3 (remember in python index start at 0) ex_list[3] = 100 ... In Python, lists and numpy arrays are mutable and you can modify list or array elements directly by updating the indexes.
Find elsewhere
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to replace an element in a python list by index
5 Best Ways to Replace an Element in a Python List By Index - Be on the Right Side of Change
February 16, 2024 - Whenever the index i equals 1, it calls fruits.__setitem__(i, 'blueberry'), which replaces the element at index i with ‘blueberry’. Method 1: Direct Assignment. Simple and most efficient way for replacing a single element. Cannot handle complex conditions easily. Method 2: pop() and insert() Methods. Allows access to the replaced element. Involves two operations, thus is less efficient than direct assignment. Method 3: List Comprehension. Pythonic and useful for replacing multiple items based on conditions.
🌐
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 Help
pythonhelp.org › python-lists › how-to-replace-an-index-in-a-list-python
How to Replace an Index in a List Python
October 16, 2023 - For instance, in case you have multiple elements which are similar but differ at some indices or you want to replace an item without knowing its exact index then ‘replace()’ method can be handy as it directly replaces the item with given value and if there is no item matching with old_value then it will return error. But remember that both methods modify the original list. Written for working developers, Coding with AI goes beyond hype to show how AI fits into real production workflows. Learn how to integrate AI into Python projects, avoid hallucinations, refactor safely, generate tests and docs, and reclaim hours of development time—using techniques tested in real-world projects.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › python-ndash-multiple-indices-replace-in-string
Python – Multiple Indices Replace in String
September 1, 2023 - We iterate through each index and build a new string by concatenating parts before the index, the replacement character, and parts after the index. Define the original string and indices to replace · Iterate through each index · Use string ...
🌐
FavTutor
favtutor.com › blogs › replace-character-string-python
Python Replace Character in String | FavTutor
October 6, 2021 - Just like the above example, you can make use of for loop to iterate through string characters and replace them using the slicing method. Take a look at the below example for a better understanding. ... 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)
Top answer
1 of 9
57

The biggest problem with your code is that it's unreadable. Python code rule number one, if it's not readable, no one's gonna look at it for long enough to get any useful information out of it. Always use descriptive variable names. Almost didn't catch the bug in your code, let's see it again with good names, slow-motion replay style:

to_modify = [5,4,3,2,1,0]
indexes = [0,1,3,5]
replacements = [0,0,0,0]

for index in indexes:
    to_modify[indexes[index]] = replacements[index]
    # to_modify[indexes[index]]
    # indexes[index]
    # Yo dawg, I heard you liked indexes, so I put an index inside your indexes
    # so you can go out of bounds while you go out of bounds.

As is obvious when you use descriptive variable names, you're indexing the list of indexes with values from itself, which doesn't make sense in this case.

Also when iterating through 2 lists in parallel I like to use the zip function (or izip if you're worried about memory consumption, but I'm not one of those iteration purists). So try this instead.

for (index, replacement) in zip(indexes, replacements):
    to_modify[index] = replacement

If your problem is only working with lists of numbers then I'd say that @steabert has the answer you were looking for with that numpy stuff. However you can't use sequences or other variable-sized data types as elements of numpy arrays, so if your variable to_modify has anything like that in it, you're probably best off doing it with a for loop.

2 of 9
37

numpy has arrays that allow you to use other lists/arrays as indices:

import numpy
S=numpy.array(s)
S[a]=m
🌐
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 slice the original string till the nth index, add the new character, followed by the slice of the original string after nth index. temp = 'pen' n = 1 new_temp = temp[: n] + 'i' + temp[n + 1:] print(new_temp) # displays pin ...
🌐
Finxter
blog.finxter.com › how-to-replace-a-list-element-at-a-specific-index-in-python
How to Replace One or More List Elements at Specific Indices in Python? – Be on the Right Side of Change
March 8, 2021 - You can use the range() function to get the pair of the i-th index and the i-th replacement value in a for loop. Then, you replace all elements one-by-one. lst = ['Alice', 'Bob', 'Carl', 'Dave', 'Elena', 'Frank', 'George'] repl = ['None', 'Foo', 'Bar'] indices = [0, 2, 5] # Method 1: For Loop ...
🌐
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. text = "ABCD" replacement = "xy" position = 1 number_of_characters = 2 result = "".join((text[:position], replacement, text[position + number_of_characters:])) print(result) # AxyD ... Our content is created by volunteers - like Wikipedia.