Quick answer: str objects are immutable, so Python cannot assign to text[0] or another character position. Construct a new string with slicing or replace(), edit a list of characters for many changes, or use bytearray for mutable binary data.

TypeError: 'str' object does not support item assignment happens when Python code tries to change one character inside a string with index assignment. Python strings are immutable, so individual characters cannot be edited in place.
The official references are Python’s string type documentation and the docs for mutable sequence types.
The fix is to build a new string, or convert the text to a mutable container, change that container, and join it back into a string. Which option is best depends on whether you need one change, many changes, or byte-level editing.
This error often appears after code is translated from a language where text can be edited by index. Python makes a different tradeoff: strings are stable values, and operations such as slicing, concatenation, and replacement return new strings.
That design also makes many string operations easier to reason about. A function that receives a string cannot silently change the caller’s original text.
Why The Error Happens
This pattern fails because text[0] returns a one-character string, not a writable slot.
text = "python"
text[0] = "P"
print(text)
The assignment line is valid syntax, but it raises a TypeError when executed. Python protects strings from in-place edits so string values stay stable wherever they are reused.
Lists behave differently because list items are mutable. That difference is why converting to a list can be useful for multiple character edits.
The traceback usually points at the exact line with text[index] = value. Start there and decide whether the task is index-based, pattern-based, or byte-based.
Fix One Character With Slicing
For one replacement, slicing is usually the clearest fix. Keep the part before the index, add the new character, and keep the part after the index.
text = "python"
fixed = "P" + text[1:]
print(fixed)
This creates a new string and leaves the original string unchanged. It is short, fast enough for ordinary text edits, and easy to read.
The same idea works for a middle character by slicing around the target position.

Replace A Middle Character
When the index is not zero, use two slices around the character you want to replace.
This is a direct replacement, not an insertion. The slice after the index starts at index + 1 so the old character is skipped.
If you want to insert text without removing a character, use text[:index] + insert_text + text[index:].
For example, replacing the final character of "pythom" can be written as text[:5] + "n" + text[6:]. The same pattern works for any valid index.
Use list And join For Many Changes
If several positions need changes, convert the string to a list of characters, edit the list, and join the result.
text = "pithon"
chars = list(text)
chars[1] = "y"
chars[0] = "P"
fixed = "".join(chars)
print(fixed)
This is more convenient than repeated slicing when many indexes are involved. It also makes the order of edits obvious.
After joining, the result is a normal string again.
This is the best option when edits come from several positions because you avoid building many intermediate strings one at a time.

Use replace For Text Patterns
If you are replacing a known substring rather than a single index, str.replace() is usually better.
text = "python_pool_python"
fixed_once = text.replace("_", "-", 1)
fixed_all = text.replace("_", "-")
print(fixed_once)
print(fixed_all)
The third argument limits how many replacements are made. Without it, every matching substring is replaced.
Use replace() for named text patterns and slicing for index-based edits.
If the pattern may appear more than once, decide whether all matches should change or only the first few. The optional count argument makes that choice explicit.
Use bytearray For Byte Data
For binary data, bytearray is mutable. It is a better fit than str when you need to update raw bytes.
data = bytearray(b"python")
data[0] = ord("P")
print(data)
print(data.decode("utf-8"))
This is for byte data, not general Unicode text processing. For normal text, prefer string methods, slicing, or list conversion.
Do not convert text to bytes unless the task is actually about encoded data.

Create A Safe Helper
A helper function keeps index checks and string building in one place.
def replace_char(text, index, char):
if len(char) != 1:
raise ValueError("replacement must be one character")
if not -len(text) <= index < len(text):
raise IndexError(index)
if index < 0:
index += len(text)
return text[:index] + char + text[index + 1:]
print(replace_char("python", 0, "P"))
print(replace_char("pythom", -1, "n"))
This function returns a new string every time. That is the correct mental model for Python text updates.
The practical rule is simple: do not assign to text[index]. Use slicing for one index, replace() for patterns, list plus join() for many character edits, and bytearray only for byte data.
Once you choose the right pattern, the error disappears because the code no longer asks Python to mutate an immutable string object.
Build A New String
A string operation returns a new value instead of modifying the original object. Slicing is clear when the position is known; replace() is better when the old text is the semantic target. Assign the returned value back to the variable.
text = "python"
text = "P" + text[1:]
text = text.replace("thon", "THON")
print(text)

Use A Character List For Many Edits
Converting to a list gives each character a mutable slot. Perform a group of edits, then join the result into a new string. Validate indexes and remember that the list’s temporary mutability does not change strings themselves.
chars = list("python")
chars[0] = "P"
chars[-1] = "!"
text = "".join(chars)
print(text)
Keep Bytes Separate From Text
bytearray is mutable binary storage, while str is immutable Unicode text and bytes is immutable binary data. Choose the type based on the boundary and encoding contract rather than converting just to avoid an exception.
data = bytearray(b"abc")
data[0] = ord("A")
print(data)
text = "abc"
print(text.encode("utf-8"))
Python’s introduction notes that strings cannot be changed; replacement creates a new value.
For related text boundaries, continue with StringIO, multiline strings, and bytes decoding.
Frequently Asked Questions
Why does str not support item assignment?
Python strings are immutable, so an existing character position cannot be changed in place.
How do I replace one character in a string?
Use slicing, replace(), or a formatted construction that returns a new string.
How do I edit many characters efficiently?
Convert the string to a list when character-level edits are needed, then join the list into a new string.
What should I use for mutable binary data?
Use bytearray when the data is bytes and in-place byte edits are part of the design.