“字符串”和“字符串”之间有什么区别吗? 和“字符串” 在Python中?

发布于 2024-07-05 13:12:02 字数 76 浏览 10 评论 0 原文

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

In PHP, a string enclosed in "double quotes" will be parsed for variables to replace whereas a string enclosed in 'single quotes' will not. In Python, does this also apply?

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

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

发布评论

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

评论(9

怎言笑 2024-07-12 13:12:05

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

There is no difference in Python, and you can really use it to your advantage when generating XML. Correct XML syntax requires double-quotes around attribute values, and in many languages, such as Java, this forces you to escape them when creating a string like this:

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

But in Python, you simply use the other quote and make sure to use the matching end quote like this:

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

Pretty nice huh? You can also use three double quotes to start and end multi-line strings, with the EOL's included like this:

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'."""
七分※倦醒 2024-07-12 13:12:05

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

样式

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

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

对于三引号字符串,始终使用双引号字符
与 PEP 257 中的文档字符串约定一致。

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

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

The difference between " and ' string quoting is just in style - except that the one removes the need for escaping the other inside the string content.

Style

PEP8 recommends a consistent rule, PEP257 suggests that docstrings use triple double quotes.

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 .

Widely used however is the practice to prefer double-quotes for natural language strings (including interpolation) - thus anything which is potentially candidate for I18N. And single quotes for technical strings: symbols, chars, paths, command-line options, technical REGEXes, ...

(For example, when preparing code for I18N, I run a semi-automatic REGEX converting double quoted strings quickly for using e.g. gettext)

孤千羽 2024-07-12 13:12:05

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

There are 3 ways you can qoute strings in python:
"string"
'string'
"""
string
string
"""
they all produce the same result.

绮筵 2024-07-12 13:12:05

是的。
那些声称 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 键,您还可以消耗更少的能量。

Yes.
Those claiming single and double quotes are identical in Python are simply wrong.

Otherwise in the following code, the double-quoted string would not have taken an extra 4.5% longer for Python to process:

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)

Output:

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

So if you want fast clean respectable code where you seem to know your stuff, use single quotes for strings whenever practical. You will also expend less energy by skipping the shift key.

日久见人心 2024-07-12 13:12:04

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

示例:

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

这在 python 文档的“字符串文字”页面上有记录:

Python is one of the few (?) languages where ' and " have identical functionality. The choice for me usually depends on what is inside. If I'm going to quote a string that has single quotes within it I'll use double quotes and visa versa, to cut down on having to escape characters in the string.

Examples:

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

This is documented on the "String Literals" page of the python documentation:

年华零落成诗 2024-07-12 13:12:04

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

>>> "text"
'text'

>>> 'text'
'text'

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

The interactive Python interpreter prefers single quotes:

>>> "text"
'text'

>>> 'text'
'text'

This could be confusing to beginners, so I'd stick with single quotes (unless you have different coding standards).

沒落の蓅哖 2024-07-12 13:12:04

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'

Single and double quoted strings in Python are identical. The only difference is that single-quoted strings can contain unescaped double quote characters, and vice versa. For example:

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

Then again, there are triple-quoted strings, which allow both quote chars and newlines to be unescaped.

You can substitute variables in a string using named specifiers and the locals() builtin:

name = 'John'
lastname = 'Smith'
print 'My name is %(name)s %(lastname)s' % locals()  # prints 'My name is John Smith'
指尖凝香 2024-07-12 13:12:04

在某些其他语言中,如果使用单引号,则不会解释元字符。 以 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

In some other languages, meta characters are not interpreted if you use single quotes. Take this example in Ruby:

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

In Python, if you want the string to be taken literally, you can use raw strings (a string preceded by the 'r' character):

>>> print 'string1\nstring2'
string1
string2
>>> print r'string1\nstring2'
string1\nstring2
杯别 2024-07-12 13:12:03

2.4.1。 字符串和字节文字

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

No:

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

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