Replacing one character of a string in python

后端 未结 5 1021
傲寒
傲寒 2020-12-20 11:35

In python, are strings mutable? The line someString[3] = \"a\" throws the error

TypeError: \'str\' object does not support item assig

5条回答
  •  醉梦人生
    2020-12-20 12:27

    Python strings are immutable, which means that they do not support item or slice assignment. You'll have to build a new string using i.e. someString[:3] + 'a' + someString[4:] or some other suitable approach.

提交回复
热议问题