← Back

Home › Errors › TypeError

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

  1. 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:].
  2. Convert to a list, mutate, join back. letters = list(text) — lists *are* mutable, so letters[0] = "P" works — then text = "".join(letters). Best when you change many positions.
  3. Use the string method that already exists. text.replace("old", "new"), text.title(), text.capitalize() and text.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

Related messages worth knowing

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 नहीं।

पूरी Hindi explanation पढ़ें →