TypeError: 'str' object does not support item assignment — decoded
Exception: TypeErrorCategory: runtimeRaised at: the moment you assign into text[i]
What this message means
You tried to assign into a position of a string: text[0] = "H". In Python, strings are immutable — once created, a str object can never be modified. There is no character slot to write into; even a one-character string is a finished object.
The message is precise about this: it does not say your index is wrong or your value is wrong. It says the *operation* — item assignment — is not supported by a str, no matter what you assign. Reading text[0] is fine; writing text[0] is impossible.
Traceback (most recent call last):
File "main.py", line 2, in <module>
name[0] = "P"
~~~~^^^
TypeError: 'str' object does not support item assignment
A real example
name = "pydebug"
name[0] = "P" # capitalise the first letter
print(name)
The intent is clear and the fix is a one-liner — but not this one. You cannot repair a string in place; you have to build a new string and rebind the name to it:
name = "pydebug"
name = "P" + name[1:]
print(name) # PyDebug
The three fixes that work
- Rebuild with a slice. Replace the affected characters by concatenating around a slice:
name = "P" + name[1:]. Works for any single position:text = text[:i] + new + text[i+1:]. - Convert to a list, mutate, join back.
letters = list(text)— lists *are* mutable, soletters[0] = "P"works — thentext = "".join(letters). Best when you change many positions. - Use the string method that already exists.
text.replace("old", "new"),text.title(),text.capitalize()andtext.upper()return new strings with the change applied — usually the clearest fix.
# Fix 2 in full — the list round-trip
text = "hello world"
letters = list(text)
letters[0] = "H"
text = "".join(letters)
print(text) # Hello world
Why Python works this way
Immutability is a deliberate trade-off. Because a str can never change, Python can safely hash strings (which is why they work as dict keys), share one copy of the same literal everywhere, and guarantee that a string you passed to a function cannot be edited behind your back. Tuples and bytes are immutable for the same reason — and raise the same TypeError if you try to assign into them.
Lists are the mutable sibling: list(text) gives you characters you can edit, and "".join(...) converts back efficiently. The pattern *convert → mutate → convert back* is the standard answer whenever an immutable type blocks an in-place edit.
Common situations that trigger it
- Capitalising or masking characters — turning the first letter upper-case, or replacing digits with
*for a masked phone number. - Coming from C or C++ — where
s[0] = 'x'is legal because strings are char arrays there. - Building output inside a loop — trying to fix up a result string position-by-position instead of collecting parts in a list and
join()-ing at the end. - Editing a value you got from
.split()or slicing — the piece is still a string, so the same rule applies.
Related messages worth knowing
- TypeError: the parent guide — the five famous wordings of this exception, decoded.
- TypeError: int is not subscriptable — the sibling message when the bracket sits on a number instead of a string.
- String methods (Hindi) — the replace/title/upper alternatives explained in Hindi.
Fix it interactively
These free problems practice string mutation the Python way — repairing them drills exactly the habit this error is asking for:
Practice more Python bugs →🇮🇳 Hindi में समझें
Python में strings immutable होती हैं — एक बार बनने के बाद उनका कोई character बदला नहीं जा सकता। इसलिए text[0] = "H" कभी काम नहीं करेगा। Fix हमेशा नई string बनाकर लिखना है: "H" + text[1:], या text.replace(...), या list(text) में बदलकर edit करके "".join(...) से वापस string बनाना। पढ़ना (text[0]) allowed है, लिखना (text[0] = ...) allowed नहीं।