How to Fix the Python Error: typeerror: 'str' object does not support item assignment

People come to the Python programming language for a variety of different reasons. It’s highly readable, easy to pick up, and superb for rapid prototyping. But the language’s data types are especially attractive. It’s easy to manipulate Python’s various data types in a number of different ways. Even converting between dissimilar types can be extremely simple. However, some aspects of Python’s data types can be a little counterintuitive. And people working with Python’s strings often find themselves confronted with a “typeerror: ‘str’ object does not support item assignment” error.

The Cause of the Type Error

The “typeerror: ‘str’ object does not support item assignment” is essentially notifying you that you’re using the wrong technique to modify data within a string. For example, you might have a loop where you’re trying to change the case of the first letter in multiple sentences. If you tried to directly modify the first character of a string it’d give you a typeerror. Because you’re essentially trying to treat an immutable string like a mutable list.

A Deeper Look Into the Type Error

The issue with directly accessing parts of a string can be a little confusing at first. This is in large part thanks to the fact that Python is typically very lenient with variable manipulation. Consider the following Python code.

y = [0,1,2,3,4]
y[1] = 2
print(y)

We assign an ordered list of numbers to a variable called y. We can then directly change the value of the number in the second position within the list to 2. And when we print the contents of y we can see that it has indeed been changed. The list assigned to y now reads as [0, 2, 2, 3, 4].

We can access data within a string in the same way we did the list assigned to y. But if we tried to change an element of a string using the same format it would produce the “typeerror: ‘str’ object does not support item assignment”.

There’s a good reason why strings can be accessed but not changed in the same way as other data types in the language. Python’s strings are immutable. There are a few minor exceptions to the rule. But for the most part, modifying strings is essentially digital sleight of hand.

We typically retrieve data from a string while making any necessary modifications, and then assign it to a variable. This is often the same variable the original string was stored in. So we might start with a string in x. We’d then retrieve that information and modify it. And the new string would then be assigned to x. This would overwrite the original contents of x with the modified copy we’d made.

This process does modify the original x string in a functional sense. But technically it’s just creating a new string that’s nearly identical to the old. This can be better illustrated with a few simple examples. These will also demonstrate how to fix the “typeerror: ‘str’ object does not support item assignment” error.

How To Fix the Type Error

We’ll need to begin by recreating the typeerror. Take a look at the following code.

x = “purString”
x[0] = “O”
print (x)

The code begins by assigning a string to x which reads “purString”. In this example, we can assume that a typo is present and that it should read “OurString”. We can try to fix the typo by replacing the value directly and then printing the correction to the screen. However, doing so produces the “typeerror: ‘str’ object does not support item assignment” error message. This highlights the fact that Python’s strings are immutable. We can’t directly change a character at a specified index within a string variable.

However, we can reference the data in the string and then reassign a modified version of it. Take a look at the following code.

x = “purString”
x = “O” + x[1::]
print (x)

This is quite similar to the earlier example. We once again begin with the “purString” typo assigned to x. But the following line has some major differences. This line begins by assigning a new value to x. The first part of the assignment specifies that it will be a string, and begin with “O”.

The next part of the assignment is where we see Python’s true relationship with strings. The x[1::] statement reads the data from the original x assignment. However, it begins reading with the first character. Keep in mind that Python’s indexing starts at 0. So the character in the first position is actually “u” rather than “p”. The slice uses : to signify the last character in the string. Essentially, the x[1::] command is shorthand for copying all of the characters in the string which occur after the “p”. However, we began the reassignment of the x variable by creating a new string that starts with “O”. This new string contains “OurString” and assigns it to x.

Again, keep in mind that this functionally replaces the first character in the x string. But on a technical level, we’re accessing x to copy it, modifying the information, and then assigning it to x all over again as a new string. The next line prints x to the screen. The first thing to note when we run this code is that there’s no Python error anymore. But we can also see that the string in x now reads as “OurString”.

How to Fix the Python Error: typeerror: 'str' object does not support item assignment
Scroll to top