-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
  • 14,486
  • 4
  • 43
  • 54
khalidmfy
  • 11
  • 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
  • 1,782
  • 1
  • 2
  • 13