Is there any difference between “string” and 'string' in Python?
在PHP中,"双引号"中的字符串将被分析以替换变量,而"单引号"中的字符串则不会。在python中,这也适用吗?
不:
2.4.1. String and Bytes literals
...In plain English: Both types of literals can be enclosed in matching single quotes (
' ) or double quotes (" ). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash (\ ) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character...
python是为数不多的(?)"和"具有相同功能的语言。我的选择通常取决于里面是什么。如果我要引用一个包含单引号的字符串,我将使用双引号,反之亦然,以减少字符串中字符的转义。
实例:
1 2 | "this doesn't require escaping the single quote" 'she said"quoting is easy in python"' |
这在python文档的"string literals"页面中有记录:
- http://docs.python.org/2/reference/lexical_analysis.html字符串文本(2.x)
- http://docs.python.org/3/reference/lexical_analysis.html字符串和字节文本(3.x)
在某些其他语言中,如果使用单引号,则不会解释元字符。以Ruby为例:
1 2 3 4 5 6 7 8 9 10 | irb(main):001:0> puts"string1 string2" string1 string2 => nil irb(main):002:0> puts 'string1 string2' string1 string2 => nil |
在python中,如果希望字面上取字符串,可以使用原始字符串(前面有"r"字符的字符串):
1 2 3 4 5 6 7 8 | >>> print 'string1 string2' string1 string2 >>> print r'string1 string2' string1 string2 |
"和"字符串引用之间的区别只是风格上的不同——除了一个消除了在字符串内容中转义另一个字符串的需要。
风格PEP8建议使用一致的规则,PEP257建议DocString使用三重双引号。
In Python, single-quoted strings and double-quoted strings are the
same. This PEP does not make a recommendation for this. Pick a rule
and stick to it. When a string contains single or double quote
characters, however, use the other one to avoid backslashes in the
string. It improves readability.For triple-quoted strings, always use double quote characters to be
consistent with the docstring convention in PEP 257 .
然而,习惯上更喜欢自然语言字符串的双引号(包括内插),因此任何可能适用于i18n的字符串都可以采用单引号。技术字符串的单引号包括:符号、字符、路径、命令行选项、技术正则表达式等等。
(例如,在为i18n准备代码时,我运行了一个半自动regex,快速转换双引号字符串以供使用,例如
交互式python解释器喜欢单引号:
10对于初学者来说,这可能会令人困惑,所以我坚持使用单引号(除非您有不同的编码标准)。
python中的单引号和双引号字符串是相同的。唯一的区别是,单引号字符串可以包含无范围双引号字符,反之亦然。例如:
1 2 | 'a"quoted" word' "another 'quoted' word" |
另外,还有三重引号的字符串,它们允许引号字符和换行符都是无范围的。
可以使用命名说明符和locals()内置项替换字符串中的变量:
1 2 3 | name = 'John' lastname = 'Smith' print 'My name is %(name)s %(lastname)s' % locals() # prints 'My name is John Smith' |
在Python中没有区别,在生成XML时,您可以真正利用它。正确的XML语法需要围绕属性值的双引号,并且在许多语言中,例如Java,这迫使您在创建这样的字符串时逃脱它们:
1 | String HtmlInJava ="<body bgcolor="Pink">" |
但在python中,只需使用另一个引号,并确保使用匹配的结束引号,如下所示:
1 | html_in_python = '<body bgcolor="Pink">' |
很好啊?您还可以使用三个双引号来开始和结束多行字符串,其中包括以下EOL:
1 2 3 4 5 | multiline_python_string =""" This is a multi-line Python string which contains line breaks in the resulting string variable, so this string has a ' ' after the word 'resulting' and the first word 'word'.""" |
在python中有三种方法可以计算字符串:"弦""弦""一串一串"它们都产生相同的结果。
对。在Python中,那些声明单引号和双引号相同的声明是完全错误的。
否则,在以下代码中,双引号字符串处理python不会花费额外的4.5%的时间:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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) |
输出:
1 2 3 | >python_quotes_test.py Time using single quotes: 13.9079978466 Time using double quotes: 14.5360121727 |
因此,如果您想要快速清理那些您似乎了解您的东西的、值得尊敬的代码,请在任何可行的时候对字符串使用单引号。你也将通过跳过移位键消耗更少的能量。