如何用双引号替换字符串中的单引号

2024-04-26 09:52:09 发布

您现在位置:Python中文网/ 问答频道 /正文

例如:

输入:

{'match_all':{}}

输出:

{'"match_all"':{}}

有没有正则表达式可以这样做

我知道我可以遍历字符串,每当我遇到一个键时,用“'”后跟“'”替换它的每一边;不过,我想知道你们中是否有人知道一种更为恶作剧的方法


Tags: 方法字符串matchall恶作剧
1条回答
网友
1楼 · 发布于 2024-04-26 09:52:09

为什么不尝试使用以下方法:https://www.tutorialspoint.com/python/string_replace.htm并尝试替换“for”和第二个“for”

str = "this is string example....wow!!! this is really string"
print str.replace("is", "was")
print str.replace("is", "was", 3)

输出返回:

thwas was string example....wow!!! thwas was really string

thwas was string example....wow!!! thwas is really string

print str.replace("'", "'"")
print str.replace("'", ""'", 1)

根据需要使用''以避免错误

相关问题 更多 >