这段简单的代码旨在将指定位置的分号替换为冒号,但是并没有起作用:
for i in range(0,len(line)):
if (line[i]==";" and i in rightindexarray):
line[i]=":"
它会报错
line[i]=":"
TypeError: 'str' object does not support item assignment
我该如何解决这个问题,把分号替换成冒号?使用 replace 函数并不可行,因为它不接受索引参数。有些分号我可能并不想要替换。
示例
在字符串中,我可能有任意数量的分号,例如“嘿!; 你好; !;”
我知道要替换哪些分号(我已经知道它们在字符串中的索引)。使用 replace 函数不起作用,因为我不能在其中使用索引。
str.replace()
BIF 吗? - LarsVegas