【发布时间】:2024-01-07 00:43:01
【问题描述】:
在 PHP 中,将解析用“双引号”括起来的字符串以替换要替换的变量,而用“单引号”括起来的字符串则不会。在 Python 中,这是否也适用?
【问题讨论】:
标签: python string quotes double-quotes
在 PHP 中,将解析用“双引号”括起来的字符串以替换要替换的变量,而用“单引号”括起来的字符串则不会。在 Python 中,这是否也适用?
【问题讨论】:
标签: python string quotes double-quotes
No:
2.4.1。字符串和字节字面量
...用简单的英语:两种类型的文字都可以用匹配的单引号 (
') 或双引号 (") 括起来。它们也可以包含在三个单引号或双引号的匹配组中(这些通常称为三引号字符串)。反斜杠 (\) 字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符...
【讨论】:
Python 是少数 (?) 语言之一,其中 ' 和 " 具有相同的功能。我的选择通常取决于里面的内容。如果我要引用一个包含单引号的字符串,我会使用双引号,反之亦然,以减少必须转义字符串中的字符。
例子:
"this doesn't require escaping the single quote"
'she said "quoting is easy in python"'
这在 python 文档的“字符串文字”页面上有记录:
【讨论】:
在某些其他语言中,如果您使用单引号,则不会解释元字符。以 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 中的文档字符串约定一致。
然而,广泛使用的是对自然语言字符串(包括插值)优先使用双引号的做法 - 因此任何可能成为 I18N 候选的东西。以及技术字符串的单引号:符号、字符、路径、命令行选项、技术 REGEX、...
(例如,在为 I18N 准备代码时,我运行了一个半自动的 REGEX 快速转换双引号字符串以使用例如gettext)
【讨论】:
有 3 种方法可以在 python 中引用字符串: “细绳” '细绳' """ 细绳 细绳 """ 它们都产生相同的结果。
【讨论】:
在 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 中声称单引号和双引号相同的人是完全错误的。
否则,在以下代码中,双引号字符串不会多花 4.5% 的时间让 Python 处理:
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 键,您也将消耗更少的能量。
【讨论】: