TypeError: 'str' object does not support item assignment
You tried to change one character of a string in place. Python strings are immutable — you build a new string instead.
What it means
Reading text[0] is fine; writing text[0] = "H" is not. Strings, tuples and ints cannot be modified after creation, so every "change" is really creating a new value and rebinding the name to it. The same message for a tuple means the same thing.
Common causes
1. Changing one character
Slicing builds the new string from the pieces around the change.
Breaks
word = "hello"
word[0] = "H"Works
word = "H" + word[1:]
# or: word = word.capitalize()2. Replacing characters in a loop
Index assignment does not work, but str.replace or a join over a generator does.
Breaks
for i in range(len(s)):
if s[i] == " ":
s[i] = "_"Works
s = s.replace(" ", "_")3. Many edits to the same string
Convert to a list once, edit freely, then join back.
Breaks
s[2] = "x"
s[5] = "y"Works
chars = list(s)
chars[2] = "x"
chars[5] = "y"
s = "".join(chars)How to find it in your own code
Any time you want to modify a string, write it as s = <new string built from s>. If the edits are scattered, list(s) → edit → "".join(...) is the standard pattern.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.