如何在原始 Python 字符串中包含引号 [英] How to include a quote in a raw Python string
问题描述
考虑:
<预><代码>>>>r什么"语法错误:无效语法>>>r什么"'任何'那么我们如何获得报价而不是斜线?
并且请不要建议 r'what"ever'
,因为那样问题就变成了我们如何包含这两种类型的引号?
相关>
如果你想在字符串中使用双引号而不是单引号,你可以使用单引号作为分隔符:
r'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?
If you want to use double quotes in strings but not single quotes, you can just use single quotes as the delimiter instead:
r'what"ever'
If you need both kinds of quotes in your string, use a triple-quoted string:
r"""what"ev'er"""
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.
这篇关于如何在原始 Python 字符串中包含引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!