How do I modify a single character in a string, in Python?

前端 未结 4 1873
傲寒
傲寒 2020-12-16 13:54

How do I modify a single character in a string, in Python? Something like:

 a = \"hello\"
 a[2] = \"m\"

\'str\' object does not support ite

4条回答
  •  情话喂你
    2020-12-16 14:00

    In python, string are immutable. If you want to change a single character, you'll have to use slicing:

    a = "hello"
    a = a[:2] + "m" + a[3:]
    

提交回复
热议问题