python - 我有一个字符串,想一次性替换字符串中的所有值
问题描述
str1 = "series of sentences that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few **sentences** should be organized into paragraphs."
REPLACE_STRING = {"series":"web" , "sentence":"long paragraph"}
输出:
web of long paragraph that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few **long paragraph** should be organized into paragraphs.
基本上需要一次性用字典中存在的键替换 str 中的所有值。
解决方案
尝试这个。
for key, value in REPLACE_STRING.items():
str1 = str1.replace(key, value)
print(str1)
推荐阅读
- python - Context_processor 未定义“”类/属性(错误)
- sql - 在postgres中如何找到最短的字符串数组
- c - 在包含字符串的 typedef 结构中使用指针
- javascript - 在网格数组上动态跨越 div
- python - Python:如果键的值在同一个字典中存在多次,则合并它
- r - R中径向树状图的数据结构和包
- java - ReflectionTestUtils 是否仅适用于类的字段,而不适用于该类方法内定义的变量?
- javascript - 获取 API 不通过 post 发送数据
- javascript - 如何在 JSP 中实现机场自动完成功能?
- java - 是否可以在加载后将文本写入网页?