这个问题在这里已经有答案了:

在 PHP 中,将解析用“双引号”括起来的字符串以查找要替换的变量,而用“单引号”括起来的字符串则不会。在Python中,这也适用吗?

有帮助吗?

解决方案

:

2.4.1.字符串和字节文字

...用简单的英语来说:两种类型的文字都可以用匹配的单引号括起来 (') 或双引号 (")。它们也可以包含在三个单引号或双引号的匹配组中(这些通常称为三引号字符串)。反斜杠 (\) 字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符...

其他提示

Python 是少数(?) ' 和 " 具有相同功能的语言之一。我的选择通常取决于里面的内容。如果我要引用一个包含单引号的字符串,我将使用双引号,反之亦然,以减少必须转义字符串中的字符的情况。

例子:

"this doesn't require escaping the single quote"
'she said "quoting is easy in python"'

这在 python 文档的“String Literals”页面上有记录:

在某些其他语言中,如果使用单引号,则不会解释元字符。以 Ruby 为例:

irb(main):001:0> puts "string1\nstring2"
string1
string2
=> nil
irb(main):002:0> puts 'string1\nstring2'
string1\nstring2
=> nil

在 Python 中,如果您希望字符串按字面意思处理,则可以使用原始字符串(前面带有“r”字符的字符串):

>>> print 'string1\nstring2'
string1
string2
>>> print r'string1\nstring2'
string1\nstring2

Python 中的单引号和双引号字符串是相同的。唯一的区别是单引号字符串可以包含未转义的双引号字符,反之亦然。例如:

'a "quoted" word'
"another 'quoted' word"

另外,还有三引号字符串,它允许引号字符和换行符不被转义。

您可以使用命名说明符和内置的 locals() 替换字符串中的变量:

name = 'John'
lastname = 'Smith'
print 'My name is %(name)s %(lastname)s' % locals()  # prints 'My name is John Smith'

交互式 Python 解释器更喜欢单引号:

>>> "text"
'text'

>>> 'text'
'text'

这可能会让初学者感到困惑,所以我会坚持使用单引号(除非您有不同的编码标准)。

" 和 ' 字符串引用之间的区别仅在于风格 - 只不过其中一个不需要在字符串内容中转义另一个。

风格

PEP8 推荐一致的规则, PEP257 建议文档字符串使用三重双引号。

在Python中,单引号和双引号的字符串相同。本 PEP 并未对此提出建议。选择一条规则并坚持下去。但是,当字符串包含单个或双引号字符时,请使用另一个字符来避免字符串中的后斜切。它提高了可读性。

对于三重引用的字符串,请务必使用双引号字符与PEP 257中的DocString judent进行一致。

然而,广泛使用的做法是对自然语言字符串(包括插值)更喜欢使用双引号 - 因此任何可能成为 I18N 候选者的东西。以及技术字符串的单引号:符号、字符、路径、命令行选项、技术正则表达式……

(例如,在准备 I18N 代码时,我运行半自动 REGEX 快速转换双引号字符串,以便使用例如 gettext)

在 python 中,有 3 种方法可以引用字符串:“字符串”'字符串'“”“字符串”“”它们都产生相同的结果。

Python 中没有任何区别,您在生成 XML 时确实可以利用它来发挥自己的优势。正确的 XML 语法需要在属性值周围使用双引号,并且在许多语言(例如 Java)中,这会迫使您在创建如下字符串时对它们进行转义:

String HtmlInJava = "<body bgcolor=\"Pink\">"

但在 Python 中,您只需使用另一个引号并确保使用匹配的结束引号,如下所示:

html_in_python = '<body bgcolor="Pink">'

很不错吧?您还可以使用三个双引号来开始和结束多行字符串,并包含 EOL,如下所示:

multiline_python_string = """
This is a multi-line Python string which contains line breaks in the 
resulting string variable, so this string has a '\n' after the word
'resulting' and the first word 'word'."""

是的。那些声称 Python 中单引号和双引号相同的人是完全错误的。

否则,在以下代码中,Python 处理双引号字符串不会花费额外 4.5% 的时间:

import time

time_single = 0
time_double = 0

for i in range(10000000):
    # String Using Single Quotes
    time1 = time.time()
    str_single1 = 'Somewhere over the rainbow dreams come true'
    str_single2 = str_single1
    time2 = time.time()
    time_elapsed = time2 - time1
    time_single += time_elapsed

    # String Using Double Quotes 
    time3 = time.time()
    str_double1 = "Somewhere over the rainbow dreams come true"
    str_double2 = str_double1
    time4 = time.time()
    time_elapsed = time4 - time3
    time_double += time_elapsed

print 'Time using single quotes: ' + str(time_single)
print 'Time using double quotes: ' + str(time_double)

输出:

>python_quotes_test.py
Time using single quotes: 13.9079978466
Time using double quotes: 14.5360121727

因此,如果您想要快速、干净、受人尊敬的代码,并且您似乎知道自己的东西,请在可行的情况下对字符串使用单引号。通过跳过 Shift 键,您还可以消耗更少的能量。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top