TypeError: ‘str’ object does not support item assignment

The python error TypeError: ‘str’ object does not support item assignment occurs when you attempt to alter or modify a character in a string using assignment operator. The string is an immutable object which can’t be changed. If you attempt to change the content of a string, the error TypeError: ‘str’ object does not support item assignment will be thrown.

A string is a sequence of characters. These characters can not be assigned using assignment operator. If you want to modify a character in a string, a new string will be created. The current string can not be changed. Thus, the python strings are called immutable objects

The characters in the string are identified by the index. If you try to modify the character in the string index using the assignment operator, then the error TypeError: ‘str’ object does not support item assignment will be thrown. You can’t modify the character in the string, instead you create a new string using the character.

The python string is an unchangeable object. If the string is modified by concatenation, a new string is created instead of altering the original string. The new string will be created in new memory location. If you try to modify the immutable string, the python interpreter will throw the error TypeError: ‘str’ 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. The stack trace will display the line that the assignment operator is attempting to change the character in the string.

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]


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


How to reproduce this error

If you are attempting to change a character in a string using the assignment operator, this error can be reproduced. In the example below, the first character is tried to change in the string “hello” using the assignment operator. That is why the error will be thrown.

test.py

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]


Root Cause

The string can not be changed in Python. A new string will be created instead. The memory reference to the string will be changed if the string is changed. If a string is tried in the same memory reference using some assignment operator, the python interpreter would not allow the character to be modified in the same memory reference.



Explanation with Example

Strings are immutable, which means you can’t modify them. But you can change the memory reference to a different string. The string in the example will be added with the character “1.” After the string is changed, the location of the memory is moved to a new location. The memory reference to the old string remains same.

String example (Immutable object)

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 that you can modify it without changing the list reference. In this case , there are three elements in the list. A new element will be added to the list without altering the memory location of the list.

List example (Mutable object)

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 can not be modified. Instead, a new string will be created. You may assign the appropriate complete string using the assignment operator instead of replacing the character in the string. In the example below, the required string is assigned to the variable to resolve the error.

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

In the case of dynamic string formation, you can modify the string using build in functions such as substring. Extract the appropriate part of a string. Attach a new character to the new string. The new string will be created in the new memory location. The memory location of the old string remains same and can not be replaced with the new string.

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

Output

hello
Hello
[Finished in 0.1s]


Solution 3

If you need to modify several characters in a string, a new string will be generated in the previous method when you add each character. This is not effective when it comes to memory use. Instead, the string is converted to a list of characters, and the characters are changed and then converted back to a string.

x = "hello"
print x
lst = list(x)
lst[0] ='H'
x = ''.join(lst) 
print x

Output

hello
Hello
[Finished in 0.0s]


Solution 4

If you’re going to change the word in a sentence. The split function is used to modify the sentence. The string is divided by a space delimiter. You can change the appropriate string using the list index. Then the sentence can be reconstructed.

x = "hello world"
print x
y = x.split()
y[0]='Hello'
x = ' '.join(y)
print x

Output

hello world
Hello world
[Finished in 0.1s]



Related Articles

Leave a Reply

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