0
x = ["slithy",[7,10,12],2,"tove",1]    
x[0][:3] = 'fea'
print(x)

TypeError: 'str' object does not support item assignment

Subhodeep
  • 33
  • 1
  • 9

2 Answers2

1

You are attempting to modify the 0th element of the list, which is a string.

You would see the same error with this code

s = "slithy"
s[:3] = 'fea'

The reason for the error is that strings are immutable in Python and as such cannot be modified.

0

In Python strings are immutable. If your expected result is x = ["slifea",.....], this code is wrong and returns that error.