. Advertisement .
. Advertisement .
Python strings are immutable and cannot be changed. Thus, trying to change an existing string’s contents will lead to a TypeError: ‘str’ object does not support item assignment.
What is the problem? How to solve this common Python error? Keep following the post to get the answers.
What Is The TypeError: ‘str’ OBject Does Not Support Item Assignment?
The error message notifies that an item cannot be assigned to a sequence of characters or a string object. It means you fail to change a string value using the assignment operation.
Here is an example of this error:
#Intialising the string variable str1 str1="Stechies" #Assigning new value at index 0 str1="Python" print(str1)
'str' object does not support item assignment
How To Solve The TypeError: ‘str’ Object Does Not Support Item Assignment?
Method 1: Add Non-numeric Values
The best way to solve this problem is to add a non-numeric character to the string or index. After adding this new character, you will get the new string in the memory location.
It is worth noting down that the old one will not be replaced. Instead, it will remain in the same place.
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)
The code replaces the name() character with an empty string to create a “final_username” variable. If your character is not a number, it will be added to the end of this string.
Use the isnumeric() method to see whether the character is a number or not. Then, you should use an assignment operator to add all characters to the “final_username” string. Run the following code to remove all the numbers:
Enter a username: pythonista101 pythonista
From this moment, you are working with a new letter-based string instead of changing the value of an existing one.
# 'str' object does not support item assignment ## Problem name = input("Enter a username: ") for c in range(len(name)): if not name[c].isnumeric(): name[c] = "" print(name) ## Solution 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)
Method 2: Convert To A List
Modifying several characters will require another method. In this case, you should convert the string into a list of characters. Those characters will be changed and then converted back to the previous state later on.
Here is how it works:
x = "hello" print x lst = list(x) lst ='H' x = ''.join(lst) print x
The string works as a single inseparable value with indexes. Yet, if you convert it into a list, you can update the value easily.
#String Variable string="Hello Python" #printing Fourth index element of the String print(string) #Creating list of String elements lst=list(string) print(lst) #Assigning value to the list lst="a" print(lst) #use join function to convert list into string new_String="".join(lst) print(new_String)
hello Hello [Finished in 0.0s] Hella Python
It is undoubted that we cannot change the value of an existing string. You will only receive the TypeError: ‘str’ object does not support item assignment. So check the post to see what you can do with the error.
Leave a comment