The assignment str[i] = str[j] is working inconsistently. Please refer to the screenshots and let me know if I am missing something.
We are receiving TypeError: ‘src’ object does not support item assignment
Regards, Praveen. Thank you!
The assignment str[i] = str[j] is working inconsistently. Please refer to the screenshots and let me know if I am missing something.
We are receiving TypeError: ‘src’ object does not support item assignment
Regards, Praveen. Thank you!
Please don’t use screenshots. Show the code and the traceback as text.
Strings are immutable. You can’t modify a string by trying to change a character within.
>>> s = "foobar"
>>> s[3] = "j"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
You can create a new string with the bits before, the bits after, and whatever you want in between.
>>> s[:3] + "j" + s[4:]
'foojar'
Yeah, you cannot assign a string to a variable, and then modify the string, but you can use the string to create a new one and assign that result to the same variable. Borrowing some code from @BowlOfRed above, you can do this:
s = "foobar"
s = s[:3] + "j" + s[4:]
print(s)
Output:
foojar