python python中双引号和引号的使用区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/628657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:29:51  来源:igfitidea点击:

Difference between the use of double quote and quotes in python

python

提问by Andrew Szeto

Is there any difference between the use of double quotes to single quotes in Python?

Python中双引号和单引号的使用有什么区别吗?

"A string with double quotes"
'A string with single quotes'

Are they identical? Are there differences in how python interprets these strings?

它们相同吗?python解释这些字符串的方式有区别吗?

回答by Andrew Szeto

Short answer: almost no difference except stylistically.
Short blurb: If you don't want to escape the quote characters inside your string, use the other type. eg:

简短回答:除了风格上几乎没有区别。
简短介绍:如果您不想转义字符串中的引号字符,请使用其他类型。例如:

string1 = "He turned to me and said, \"Hello there\""

would be slightly more unsightly than saying

会比说更难看

string2 = 'He turned to me and said, "Hello there"'

The same applies to single quotes/apostrophes.

这同样适用于单引号/撇号。

回答by i_am_jorf

Not generally, you just have to be consistent within a given statement. E.g., don't do "foo'

通常情况下,您只需要在给定的语句中保持一致。例如,不要做“foo”

For further reading, see here.

如需进一步阅读,请参见此处

回答by Rook

Double quotes look distinctive. Single quotes look like single ticks. Apart from that ...

双引号看起来与众不同。单引号看起来像单引号。除此之外 ...