Python 多次向字符串添加字符串

Python 多次向字符串添加字符串,python,Python,如何多次向字符串添加内容 import re content = 'This is random text. I want to insert HELLO here --> "" and here too --> ""' for text in re.finditer('""', content): start_index = text.start() + 1 end_index = text.end()

如何多次向字符串添加内容

import re

content = 'This is random text. I want to insert HELLO here --> "" and here too --> ""'

for text in re.finditer('""', content):
    start_index = text.start() + 1
    end_index = text.end() - 1

    content = content[:start_index] + 'HELLO' + content[end_index:]

print(content)
不幸的是,上面的代码给了我以下信息:

This is random text. I want to insert HELLO here --> "HELLO" and here too HELLO--> ""

内容如何。替换(““””、“您好”)?

以下是您想要的:

string = ' geeks for geeks geeks geeks geeks' 
   
# Prints the string by replacing geeks by Geeks  
print(string.replace("geeks", '"Geeks"'))  


结果是:

"Geeks" for "Geeks" "Geeks" "Geeks" "Geeks"

您不想使用“替换”功能?请使用str.replace或简单地按相反顺序进行替换。如果您有其他更简单的解决方案,请显示me@belismau你有一些很好的答案,如果它能解决你的问题,请接受。或者,如果需要其他帮助,请进一步澄清问题。我想在“”之间插入HELLO。这不提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论-