To sum up comments and give an structured explanation:
"123"
is a string object and is immutable. the char at operation ( [x] ) on a string object in Python return a copy of the letter. A single letter is also a string.
a = "123"
b = a[1]
You now have 2 independent string objects "123"
and "2"
. Since b
is assigned to an independent string object, changing a
will have no effect to b
. If you reassign a however, your string object "123"
will be lost and there is no way to revive “1” and “3” sorry but you probably already know that.
the list object works differently.
solved Understanding Python name objects, immutable data, and memory management