How to Fix: TypeError: ‘str’ object does not support item assignment

3D illustration of a chisel breaking against an engraved stone text, representing the string item assignment error.

The TypeError str item assignment error means you tried to change a single character inside a string. It’s an easy mistake to make, but it fails because strings are Immutable in Python.

โšก Quick Fix: TypeError: ‘str’ object does not support item assignment โ€” Python String Immutability Fix with Slicing, .replace(), and list() Conversion

You tried to change a character inside a string by index โ€” Python strings are immutable, every character is locked in place after the string is created.

# WRONG โ€” strings don't support index assignment
message = "Hello"
message[0] = "J"          # TypeError: 'str' object does not support item assignment

# FIX 1 โ€” slicing + concatenation: replace a character at a specific position
message = "Hello"
new_message = "J" + message[1:]   # 'J' + 'ello'
print(new_message)                # Output: Jello

# FIX 2 โ€” .replace(): swap every occurrence of a character or substring
new_message = message.replace("H", "J")
print(new_message)                # Output: Jello

# FIX 3 โ€” list conversion: when you need multiple character-level edits
chars = list("Hello")             # ['H', 'e', 'l', 'l', 'o']
chars[0] = "J"                    # lists support item assignment
new_message = "".join(chars)
print(new_message)                # Output: Jello

Pick the fix that matches your use case โ€” the three scenarios below break down exactly when to reach for each one.

The Cause

You can access characters by index, but you can’t assign a new character to an index.

Problem Code:

message = "Hello"
message[0] = "J" # Try to change 'H' to 'J'
# CRASH! TypeError: 'str' object does not support item assignment

The Fix: Create a New String

You cannot change the original string. You must build a new string that includes your change.

Fix 1: Using Slicing + Concatenation

message = "Hello"
new_message = "J" + message[1:] # 'J' + 'ello'

print(new_message) # Output: Jello

Fix 2: Using .replace()

If you want to replace all instances of a character, .replace() is easier.

message = "Hello"
new_message = message.replace("H", "J")

print(new_message) # Output: Jello

Just remember: message.replace() doesn’t change message; it returns a new string with the replacement made.


TypeError: ‘str’ object does not support item assignment โ€” String Immutability and Three Fix Patterns

TypeError: ‘str’ object does not support item assignment enforces one rule: Python strings never change after creation. Every operation that looks like it modifies a string actually builds a new one and leaves the original untouched.

Three fix patterns cover every real-world scenario.

Slicing + concatenation handles single-position replacement. new_str = “J” + original[1:] replaces the first character. new_str = original[:i] + “X” + original[i+1:] replaces any character at index i. Use this when the position matters more than the character value.

.replace() handles value-based substitution. original.replace(“H”, “J”) swaps every occurrence of “H” with “J” in one call. It returns a new string โ€” assign the result or you lose it. Use this when you know the character or substring you want to swap, not the position.

list() conversion handles multiple targeted edits. Convert the string to a list of characters with list(my_string), apply as many index assignments as you need, then rejoin with “”.join(chars). This is the only pattern that gives you true index-level write access โ€” use it when you need to change several specific positions in one pass.

The pattern to know for performance: if you build a string character by character inside a loop, use a list to accumulate the characters and join once at the end. Concatenating strings in a loop with + creates a new string on every iteration โ€” O(nยฒ) on large inputs. The list + join pattern runs in O(n).

SLOW โ€” new string object on every iteration

result = “”
for char in data:
result += transform(char)

FAST โ€” one join at the end

chars = [transform(char) for char in data]
result = “”.join(chars)

Similar Posts

Leave a Reply