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 - The string is split into two parts: everything before the target index (text[:index]) and everything after (text[index+1:]). The replacement character is inserted between these slices.
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
Replace character at specific index
How do I change 19.800.00 to 19,800.00 More on forum.uipath.com
🌐 forum.uipath.com
4
0
October 19, 2022
Python: How can I replace one specific character on a string while leaving the rest of the string as it was?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
12
6
October 18, 2023
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
🌐
FavTutor
favtutor.com › blogs › replace-character-string-python
Python Replace Character in String | FavTutor
October 6, 2021 - For this approach, you can make use of for loop to iterate through a string and find the given indexes. Later, the slicing method is used to replace the old character with the new character and get the final output.
🌐
TutorialsPoint
tutorialspoint.com › article › python-program-to-change-character-of-a-string-using-given-index
Python program to change character of a string using given index
March 26, 2026 - 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.
🌐
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…
Find elsewhere
🌐
Quora
quora.com › How-do-I-replace-characters-in-a-string-in-Python
How to replace characters in a string in Python - Quora
Answer (1 of 4): By using replace() method The replace() method replaces all occurrences of a character with the new one. For instance, if we have a string any_string and we use the expression any_string.replace(‘a’, ‘b’), it will replace all occurrences of the character ‘a’ in ...
🌐
StrataScratch
stratascratch.com › blog › how-to-replace-a-character-in-a-python-string
How to Replace a Character in a Python String - StrataScratch
October 18, 2024 - There are several ways to replace a character in a Python string. In this article, you’ll learn three main techniques and how to use them in practice.
🌐
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 ...
🌐
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.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python replace character in string
Python Replace Character in String - Spark By {Examples}
May 21, 2024 - How to replace a character in a string in Python? To replace a character in a string using Python, you can utilize the str.replace() method. This method
🌐
Elixir Forum
elixirforum.com › t › replacing-characters-in-a-string-at-a-specific-index › 44065
Replacing characters in a String at a specific Index | Elixir Forum
September 19, 2022 - I would write a recursive function that goes through the string once and replaces the character as it goes. Something like (assuming the indices are sorted): defmodule Replacer do def replace(string, indices) do replace(string, indices, 0, "") end def replace(string, [], _current, acc) do acc <> string end def replace(string, [index | indices], current, acc) do case String.next_grapheme(string) do nil -> acc {next, rest} -> if index == current do replace(rest, indices, current + 1, acc <> "_") else replace(rest, [index | indices], current + 1, acc <> next) end end end end
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence ...
🌐
Real Python
realpython.com › replace-string-python
How to Replace a String in Python – Real Python
October 22, 2025 - In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.
🌐
Quora
quora.com › How-do-you-replace-the-first-two-characters-of-a-string-in-Python
How to replace the first two characters of a string in Python - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
W3Schools
w3schools.com › python › ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The replace() method replaces a specified phrase with another specified phrase.
🌐
Sololearn
sololearn.com › en › Discuss › 2906085 › how-do-you-replace-a-char-to-another-index-on-the-same-string
How do you replace a char to another index on the same ...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
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....