如何在原始 Python 字符串中包含引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:37:32  来源:igfitidea点击:

How to include a quote in a raw Python string

pythonsyntax

提问by mpen

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?

并且请不要建议r'what"ever',因为那样问题就变成了我们如何包含这两种类型的引号?

Related

有关的

采纳答案by Adam Rosenfield

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.

如果你想在你的字符串中包含这两种三引号字符串(一种极不可能的情况),你不能这样做,你必须使用带有转义的非原始字符串。

回答by Karl

Python has more than one way to do strings. The following string syntax would allow you to use double quotes:

Python 有不止一种方法来处理字符串。以下字符串语法将允许您使用双引号:

'''what"ever'''

回答by mpen

Nevermind, the answer is raw triple-quoted strings:

没关系,答案是原始的三引号字符串:

r"""what"ever"""

回答by Bakuriu

If you need any type of quoting (single, double, and triple for both) you can "combine"(0) the strings:

如果您需要任何类型的引用(单、双和三),您可以“组合”(0)字符串:

>>> raw_string_with_quotes = r'double"' r"single'" r'''double triple""" ''' r"""single triple''' """
>>> print raw_string_with_quotes
double"single'double triple""" single triple'''

You may also "combine"(0) raw strings with non-raw strings:

您还可以“组合”(0) 原始字符串与非原始字符串:

>>> r'raw_string\n' 'non-raw string\n'
'raw_string\nnon-raw string\n'


(0): 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.

(0):其实Python解析器是对字符串进行join,并不会创建多个字符串。如果添加“+”运算符,则会创建并组合多个字符串。

回答by zulfi123786

Use:

用:

dqote='"'
sqote="'"

Use the '+' operator and dqoteand squotevariables to get what you need.

使用“+”操作符和dqotesquote变量来得到你所需要的。

If I want sed -e s/",u'"/",'"/g -e s/^"u'"/"'"/, you can try the following:

如果我愿意sed -e s/",u'"/",'"/g -e s/^"u'"/"'"/,您可以尝试以下操作:

dqote='"'
sqote="'"
cmd1="sed -e s/" + dqote + ",u'" + dqote + "/" + dqote + ",'" + dqote + '/g -e s/^"u' + sqote + dqote + '/' + dqote + sqote + dqote + '/'

回答by charrold303

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:

由于我偶然发现了这个答案,它对我帮助很大,但我发现了一个小语法问题,我觉得我应该避免其他人可能遇到的挫折。如上所述,三引号字符串适用于这种情况,但请注意,如果字符串中的 " 出现在字符串本身的末尾:

somestr = """This is a string with a special need to have a " in it at the end""""

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.

您将在执行时遇到错误,因为连续的 """" (4) 引号会混淆字符串阅读器,因为它认为它已经到达字符串的末尾,然后在那里找到一个随机的 "。您可以验证这一点通过在 4 个引号中插入一个空格,如下所示:" """ 并且它不会有错误。

In this special case you will need to either use:

在这种特殊情况下,您需要使用:

somestr = 'This.....at the end"'

or use the method described above of building multiple strings with mixed " and ' and then concatenating them after the fact.

或者使用上面描述的方法,用混合的 " 和 ' 构建多个字符串,然后在事后将它们连接起来。

回答by illusionx

Just to include new Python f String compatible functionality:

只是为了包含新的 Python f String 兼容功能:

var_a = 10

f"""This is my quoted variable: "{var_a}". """