TypeError: ‘str’ object does not support item assignment

In python, the error TypeError: ‘str’ object does not support item assignment causes when you try to modify an immutable object such as string, tuples, etc. The immutable object can not be modified instead new object will be created.

Changing the immutable object like string, tuple, e.g. changing the character in string, changing the element in the tuple will trigger the error. In python, the immutable objects are boolean, tuple, int, float, string. The list in the python is an example for the mutable object.

The string, in python, is an immutable object. If the string is modified like concatenation, instead of modifying the existing string the new string is created. The new string will be generated in new location of the memory. If you try to modify the immutable string, the python interpreter will throw the type error.

The string is created in python, and can not be modified. If you are attempting to modify a python string the python interpreter will throw the error “TypeError: ‘str’ object does not support item assignment”.



Different Variations in the error

There are different variations of this type of error due to different data types. The solutions to these errors are the same.

TypeError: 'str' object does not support item assignment
TypeError: 'tuple' object does not support item assignment
TypeError: 'int' object does not support item assignment
TypeError: 'float' object does not support item assignment
TypeError: 'bool' object does not support item assignment


Exception

The error “TypeError: ‘str’ object does not support item assignment” will be shown as below the stack trace.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 5, in <module>
    x[0] = 'H'
TypeError: 'str' object does not support item assignment
[Finished in 0.1s with exit code 1]


Root Cause

The root cause of the type error is due to the attempt to modify the immutable object within the code. The immutable object can not be modified. Instead, a new object will be created. The old memory location is changed to the new memory location.

Strings are immutable which means you can not edit them. but you can change the string reference to different string. The string in the example is added with a character “1”. After the string is modified, the memory location is changed to a new location. The reference of the old string remains same.

x = "hello"
y=x
print "x=",x," y=",y
print "Before Memory location x=", id(x), " y=", id(y)
x=x+"1"
print "x=",x," y=",y
print "After  Memory location x=", id(x), " y=", id(y)

Output

x= hello  y= hello
Before Memory location x= 4407923936  y= 4407923936
x= hello1  y= hello
After  Memory location x= 4407923696  y= 4407923936

List is a mutable object which means you can edit them without changing the reference of the list. In this example, the list contains 3 elements. A new element is added to the list without changing the memory location of the list.

x=[1,2,3]
y=x
print "x=",x," y=",y
print "Before Memory location x=", id(x), " y=", id(y)

x.append(4)
print "x=",x," y=",y
print "After  Memory location x=", id(x), " y=", id(y)

Output

x= [1, 2, 3]  y= [1, 2, 3]
Before Memory location x= 4468592224  y= 4468592224
x= [1, 2, 3, 4]  y= [1, 2, 3, 4]
After  Memory location x= 4468592224  y= 4468592224


Solution 1

The immutable string cannot be modified. Instead, a new string will be created. In the below example, the error is thrown due to the immutable string is attempted to modify.

Program

x = "hello"
print x
x[0] = 'H'
print x

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    x[0] = 'H'
TypeError: 'str' object does not support item assignment
[Finished in 0.1s with exit code 1]

Solution

x = "hello"
print x
x = 'Hello' 
print x

Output

hello
Hello
[Finished in 0.1s]


Solution 2

You can modify the string by adding the new characters with string substring. The string will be created in the new memory location. The old memory location is not modified with the new string.

Program

x = "hello"
print x
x[0] = 'H'
print x

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    x[0] = 'H'
TypeError: 'str' object does not support item assignment
[Finished in 0.1s with exit code 1]

Solution

x = "hello"
print x
x = 'H' + x[1:5] 
print x

Output

hello
Hello
[Finished in 0.1s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *