如何在原始 Python 字符串文字中包含双引号和/或单引号字符?
考虑一下:
>>> r"what"ever"
SyntaxError: invalid syntax
>>> r"what\"ever"
'what\\"ever'
那么我们如何获得引号而不是斜线呢?
并且请不要建议r'what"ever'
,因为这样问题就变成了我们如何包含这两种类型的引号?
Consider:
>>> r"what"ever"
SyntaxError: invalid syntax
>>> r"what\"ever"
'what\\"ever'
So how do we get the quote, but not the slash?
And please don't suggest r'what"ever'
, because then the question just becomes how do we include both types of quotes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想在字符串中使用双引号而不是单引号,则可以使用单引号作为分隔符:
如果您需要在字符串中使用两种引号,请使用三引号字符串:
如果您想包含两种引号字符串中的三引号字符串(极不可能的情况),您不能这样做,并且必须使用带有转义的非原始字符串。
If you want to use double quotes in strings but not single quotes, you can just use single quotes as the delimiter instead:
If you need both kinds of quotes in your string, use a triple-quoted string:
If you want to include both kinds of triple-quoted strings in your string (an extremely unlikely case), you can't do it, and you'll have to use non-raw strings with escapes.
如果您需要任何类型的引用(单引号、双引号和三引号),您可以“组合”[1] 字符串:
您也可以“组合”[1] 原始字符串与非原始字符串:
[1] 事实上,Python 解析器将字符串连接起来,并且不会创建多个字符串。如果添加“+”运算符,则会创建并组合多个字符串。
If you need any type of quoting (single, double, and triple for both) you can "combine"[1] the strings:
You may also "combine"[1] raw strings with non-raw strings:
[1] In fact, the Python parser joins the strings, and it does not create multiple strings. If you add the "+" operator, then multiple strings are created and combined.
Python 有不止一种处理字符串的方法。以下字符串语法允许您使用双引号:
Python has more than one way to do strings. The following string syntax would allow you to use double quotes:
由于我偶然发现了这个答案,它对我有很大帮助,但我发现了一个小语法问题,我觉得我应该避免其他人可能遭受的挫败感。正如所描述的,三重引号字符串适用于这种情况,但请注意,如果您想要在字符串中出现的 " 出现在字符串本身的末尾:
您将在执行时遇到错误,因为 """" (4) 中的引号row 使字符串读取器感到困惑,因为它认为它已经到达字符串的末尾,然后在那里找到一个随机的“。您可以通过在 4 个引号中插入一个空格来验证这一点,如下所示:“”“”,这样就不会出现错误。
在这种特殊情况下,您将需要使用:
或使用上述方法,使用混合的“和'构建多个字符串,然后在事后将它们连接起来。
Since I stumbled on this answer, and it greatly helped me, but I found a minor syntactic issue, I felt I should save others possible frustration. The triple quoted string works for this scenario as described, but note that if the " you want in the string occurs at the end of the string itself:
You will hit an error at execution because the """" (4) quotes in a row confuses the string reader, as it thinks it has hit the end of the string already and then finds a random " out there. You can validate this by inserting a space into the 4 quotes like so: " """ and it will not have the error.
In this special case you will need to either use:
or use the method described above of building multiple strings with mixed " and ' and then concatenating them after the fact.
没关系,答案是原始的三引号字符串:
Nevermind, the answer is raw triple-quoted strings:
只是为了包含新的 Python f String 兼容功能:
Just to include new Python f String compatible functionality:
使用:
使用“+”运算符以及
dqote
和squote
变量来获取您需要的内容。如果我想要 sed -es/",u'"/",'"/g -es/^"u'"/"'"/,您可以尝试以下操作:
Use:
Use the '+' operator and
dqote
andsquote
variables to get what you need.If I want
sed -e s/",u'"/",'"/g -e s/^"u'"/"'"/
, you can try the following: