Today, we will see what is “TypeError: ‘str’ object does not support item assignment” and will also learn how to solve it. We will see the solution to this error using 5 examples.
Being an interpreted language, the Python interpreter checks the code line by line and throws any error occurring within the line. This is unlike the compiler as the compiler displays all errors and warnings altogether.
There are mainly 3 types of can be happened errors in Python:
- Syntax Errors: The syntax of the code might not be as it should be. Eg. Missing colon or brackets.
- Exceptions: It throws when the interpreter cannot execute the requested action or line of code.
- Logical Errors: These can be human errors i.e. the output is not as expected
To the scope of this article, we will study about TypeError which is an exception in Python.
- TypeError is an exceptional error i.e. the interpreter cannot execute the requested action or line of code.
- Generally, TypeError occurs when the data type of an object in an operation or action is inappropriate. In other words, if the datatype is wrong. The object you are operating with is out of its scope to operate over the code.
- A simpler example could be if the code states to subtract an integer from a string.
Let’s understand this TypeError with examples
my_string = "copyassignment" my_integer = 5 print(my_string - my_integer)
Traceback (most recent call last): File "", line 4, in TypeError: unsupported operand type(s) for -: 'str' and 'int'
However, it’s totally different scope if you are trying to multiply or add a string with an integer because it is basically one of the properties of the data type string.
Now, as the TypeError is clear, we can really understand what does, TypeError: ‘str’ object does not support item assignment means.
Now, it has been crystal clear that this throw of an exception is due to the invalid datatype for an operation.
Exactly, the invalid datatype is ‘string’ here and the operation that has been executed is ‘item assignment’. Now, item assignment here means, assigning an item to the existing item, moreover ‘updating’ the item. On the code view, how does this error occur? Here’s how:
my_string = "mango" my_string = 'f' print(my_string)
#Output Traceback (most recent call last): File "<string>", line 2, in <module> TypeError: 'str' object does not support item assignment
The error occurred because, in Python, strings are immutable. Immutable objects are those objects which cannot be modified/updated/changed or even deleted, once created.
Every variable in Python is an object of the class of its datatype. A string variable is an instance of class ‘str’. And the elements or characters in it can’t be altered once they are defined. Another data type that is immutable in Python is a tuple and hence, doesn’t support item assignment.
And hence the TypeError: ‘str’ object does not support item assignment.
So, in c or c++ or any other programming language: (For eg. C++):
string my_string = "mango"; my_string='f'; cout<<my_string
But in Python, it won’t work due to the property of immutability of the string class.
Solution to “TypeError: ‘str’ object does not support item assignment“
One way to fix the error could be by creating a new string with the required change.
my_string = "mango" new_string = "fango" print(my_string) print(new_string)
The above code can be also written specifically for the index as-
my_string = "mango" new_string = "" for i in range(len(my_string)): if(i==0): new_string+='f' else: new_string+=my_string[i] print(new_string)
Hence, it is recommended to use mutable data types like lists, sets, or dictionaries if there’s a need to alter the elements. But if it is strict or must that the elements should not be changed, immutable datatypes are practiced in Python.
Thank you for visiting our website.
- Print Colored Text Output in Python
- Make money with Python in 2022
- File Explorer in Python using Tkinter
- Drawing Application in Python Tkinter
- Top 32 Python Interview Questions For Experienced
- Top 10 Final Year Projects for Computer Science Students
- Tkinter GUI Calendar in Python
- Microsoft Giving Free Python Course: Enroll Now
- Top 5 Free Python Courses on YouTube in 2022
- CS Class 12th Python Projects
- Google Giving Free Python Course: Enroll Now
- How to extract YouTube comments using Python?
- 15 Deep Learning Projects for Final year
- Python Alarm Clock | Tkinter GUI App
- Draw Flag of USA using Python Turtle
- Draw Iron Man Face with Python Turtle: Tony Stark Face
- Draw TikTok Logo with Python Turtle
- Draw Instagram Logo using Python Turtle
- Complete Python Roadmap for Beginners in 2022
- Games in Python | Assignment Expert
- Top 10 Python Projects for Final year Students
- 10 Tkinter Projects for Beginners
- Vending Machine with Python Code
- I Love You Text in ASCII Art
- Python Turtle Shapes- Square, Rectangle, Circle
- Python OOP Projects | Source code and example
- Python Turtle Commands and All Methods
- Generate QR Code in Python
- Make Minecraft in Python
- Battleship Game Code in Python