-2
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
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
khalidmfy
  • 15
  • 1

1 Answers1

1

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.

Lecdi
  • 2,189
  • 2
  • 6
  • 20