HOWTO · Python

How to Replace Character in a String in Python

Learn how to replace one character by position or replace matching characters in a Python string.

On this page

Python strings are immutable, so you cannot assign a new character directly to an existing string. Instead, create a new string from the changed value. Use a list when you need to edit one position, replace() when you need to change matching text, or slicing when the position is already known. A bytearray() is suitable for byte-oriented data such as ASCII, but its indexes refer to bytes rather than general Unicode characters.

Use list() and join() to replace a character in a string

Convert the string with the built-in list() function, assign the replacement at its zero-based index, and join the characters back together. Call join() on a separator string to combine the list items; "" inserts no separator. For example, index 2 refers to the third character:

text = "Naze"
characters = list(text)
characters[2] = "m"
updated = "".join(characters)
print(updated)
Name

This approach changes one position and leaves the original string value intact; updated refers to the newly joined text. The index must identify an item in the list, so check the string’s length when a position comes from user input.

Use bytearray() to replace a character in a string

For byte-oriented text, encode the string first, make an integer byte assignment in a mutable bytearray(), then decode the bytes to get a string again. This example uses ASCII, where each shown character is represented by one byte:

text = "Naze"
data = bytearray(text, "ascii")
data[2] = ord("m")
updated = data.decode("ascii")
print(updated)
Name

Encoding and decoding must use a compatible character encoding. A byte index does not generally match a Unicode character index: a character outside ASCII can occupy multiple bytes in UTF-8. Use this technique only when the data’s encoding and byte positions are part of the task; use the string-based approaches for ordinary text.

Use the replace() method to replace matching characters in a string

Call the string method replace() with the old and new text. By default, it changes every non-overlapping occurrence of the old text and returns the result as a new string:

text = "Naze"
updated = text.replace("z", "m")
print(updated)
Name

The original text remains unchanged. If only a limited number of matches should change, pass that number as the optional third argument, for example text.replace("a", "o", 1). If the old text is absent, replace() returns text with no change; this can help distinguish a spelling mismatch from an assignment problem.

Use slicing and concatenation to replace a character at a known position

When you know the zero-based position, take the prefix before it, add the replacement, and append the suffix after it. The slice text[:2] keeps positions before index 2, while text[3:] starts after that character:

text = "Naze"
updated = text[:2] + "m" + text[3:]
print(updated)
Name

This builds a new string while preserving the surrounding prefix and suffix. Make sure the suffix starts one position after the character being replaced; using the wrong index can omit or duplicate a character. Choose this form when the position is already known and the expression is clear for your use case; there is no speed comparison here.