txt = "hello"
txt[0], txt[1] = txt[1], txt[0]
why doesn't this code work?
Isn't it similar to this?
a, b = 1,2
a,b = b,a
txt = "hello"
txt[0], txt[1] = txt[1], txt[0]
why doesn't this code work?
Isn't it similar to this?
a, b = 1,2
a,b = b,a
In Python, strings do not support the syntax str[num] = ...
at all. The problem is not that you are trying to swap values, it is just that strings cannot be edited like that at all.