如何在原始 Python 字符串文字中包含双引号和/或单引号字符?

发布于 2024-10-10 18:43:47 字数 361 浏览 3 评论 0原文

考虑一下:

>>> 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?

Related

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

如果您想在字符串中使用双引号而不是单引号,则可以使用单引号作为分隔符:

r'what"ever'

如果您需要在字符串中使用两种引号,请使用三引号字符串:

r"""what"ev'er"""

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

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.

雄赳赳气昂昂 2024-10-17 18:43:47

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

>>> 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'''

您也可以“组合”[1] 原始字符串与非原始字符串:

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

[1] 事实上,Python 解析器将字符串连接起来,并且不会创建多个字符串。如果添加“+”运算符,则会创建并组合多个字符串。

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

>>> 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"[1] raw strings with non-raw strings:

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

[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.

不语却知心 2024-10-17 18:43:47

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

'''what"ever'''

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

'''what"ever'''
叹沉浮 2024-10-17 18:43:47

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

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

您将在执行时遇到错误,因为 """" (4) 中的引号row 使字符串读取器感到困惑,因为它认为它已经到达字符串的末尾,然后在那里找到一个随机的“。您可以通过在 4 个引号中插入一个空格来验证这一点,如下所示:“”“”,这样就不会出现错误。

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

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

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

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.

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.

北方。的韩爷 2024-10-17 18:43:47

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

r"""what"ever"""

Nevermind, the answer is raw triple-quoted strings:

r"""what"ever"""
一梦浮鱼 2024-10-17 18:43:47

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

var_a = 10

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

Just to include new Python f String compatible functionality:

var_a = 10

f"""This is my quoted variable: "{var_a}". """
穿透光 2024-10-17 18:43:47

使用:

dqote='"'
sqote="'"

使用“+”运算符以及dqotesquote变量来获取您需要的内容。

如果我想要 sed -es/",u'"/",'"/g -es/^"u'"/"'"/,您可以尝试以下操作:

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

Use:

dqote='"'
sqote="'"

Use the '+' operator and dqote and squote variables to get what you need.

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

dqote='"'
sqote="'"
cmd1="sed -e s/" + dqote + ",u'" + dqote + "/" + dqote + ",'" + dqote + '/g -e s/^"u' + sqote + dqote + '/' + dqote + sqote + dqote + '/'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文