Python-替换字符串中字符的实例
发布于 2021-02-02 23:15:02
这个简单的代码仅尝试用冒号替换分号(在i指定的位置)不起作用:
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不起作用,因为该函数不占用索引-可能有一些我不想替换的分号。
例
在字符串中,我可能有许多分号,例如“ Hei der!; Hello there;!;”
我知道我想替换哪些(我在字符串中有索引)。使用替换无法正常工作,因为我无法对其使用索引。
关注者
0
被浏览
106
1 个回答