. Advertisement .
..3..
. Advertisement .
..4..
It’s terrible! I encounter this error ”typeerror: ‘str’ object does not support item assignment” when I run this code below:
text = "hello world"
if text[0].islower():
text[0] = text[0].upper()
Then I get this error message:
TypeError: 'str' object does not support item assignment.
Does anyone have any suggestions for this problem:”typeerror: ‘str’ object does not support item assignment” – How to correct it?
The cause: This error happens because strings in Python are immutable and cannot be modified in place.
Solution: You can fix this error by using a string manipulation method built within Python. In this particular case,
capitalize()
is what you’ll need.Keep in mind that
capitalize
generates a new string and returns it. We need to add the new string to thetext
variable for that reason. The text in the example below would not change, butcapitale
would result in thetext
variable being capitalized.