python如何改字符串 字符串不可按索引赋值修改以下是目前掌握修改字符串的方式原句子I think i can make it.要求改成I sware i can make it.方式1使用replace替换s i think i can make it print(s.replace(think,swear))方式2使用切片的方式进行替换本质是拼接字符串s i think i can make it print(s[:2] swear s[7:])方式3使用io.StingIO构造实例化流对象创建子类实例对文本进行操作。import io s i think i can make it NewS io.StringIO(s) print(NewS.getvalue()) NewS.seek(2) NewS.write(swear) print(NewS.getvalue())方式4使用列表获取字符挨个替换本质是拼接字符串s i think i can make it lst list(s) # 复制成可改的列表原s不动 print(lst) lst[2] s lst[3] w lst[4] e lst[5] a lst[6] r s .join(lst) # 再拼回新字符串 print(s)