Strings in Python are immutable. This means that they cannot be changed. If you try to change the contents of an existing string, you’re liable to find an error that says something like “‘str’ object does not support item assignment”.
In this guide, we’re going to talk about this common Python error and how it works. We’ll walk through a code snippet with this error present so we can explore how to fix it.
The Problem: ‘str’ object does not support item assignment
Let’s start by taking a look at our error: Typeerror: ‘str’ object does not support item assignment.
This error message tells us that a string object (a sequence of characters) cannot be assigned an item. This error is raised when you try to change the value of a string using the assignment operator.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
The most common scenario in which this error is raised is when you try to change a string by its index values. The following code yields the item assignment error:
string = "Banana" string = "A"
You cannot change the character at the index position 0 because strings are immutable.
You should check to see if there are any string methods that you can use to create a modified copy of a string if applicable. You could also use slicing if you want to create a new string based on parts of an old string.
An Example Scenario
We’re going to write a program that checks whether a number is in a string. If a number is in a string, it should be replaced with an empty string. This will remove the number. Our program is below:
name = input("Enter a username: ") for c in range(len(name)): if not name[c].isnumeric(): name[c] = "" print(name)
This code accepts a username from the user using the input() method. It then loops through every character in the username using a for loop and checks if that character is a number. If it is, we try to replace that character with an empty string. Let’s run our code and see what happens:
Enter a username: pythonista101 Traceback (most recent call last): File "main.py", line 5, in <module> name[c] = "" TypeError: 'str' object does not support item assignment
Our code has returned an error.
The cause of this error is that we’re trying to assign a string to an index value in “name”:
name[c] = ""
We can solve this error by adding non-numeric characters to a new string. Let’s see how it works:
name = input("Enter a username: ") final_username = "" for c in range(len(name)): if not name[c].isnumeric(): final_username += name[c] print(final_username)
This code replaces the character at name[c] with an empty string.
We have created a separate variable called “final_username”. This variable is initially an empty string. If our for loop finds a character that is not a number, that character is added to the end of the “final_username” string. Otherwise, nothing happens. We check to see if a character is a number using the isnumeric() method.
We add a character to the “final_username” string using the addition assignment operator. This operator adds one value to another value. In this case, the operator adds a character to the end of the “final_username” string.
Let’s run our code:
Enter a username: pythonista101 pythonista
Our code successfully removed all of the numbers from our string. This code works because we are no longer trying to change an existing string. We instead create a new string called “final_username” to which we add all the letter-based characters from our username string.
In Python, strings cannot be modified. You need to create a new string based on the contents of an old one if you want to change a string.
The “‘str’ object does not support item assignment” error tells you that you are trying to modify the value of an existing string.
Now you’re ready to solve this Python error like an expert.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.