在python中使用双引号和引号之间的区别

17 python

在Python中使用双引号与单引号之间有什么区别吗?

"A string with double quotes"
'A string with single quotes'
Run Code Online (Sandbox Code Playgroud)

它们是一样的吗?python如何解释这些字符串有什么不同吗?

And*_*eto 23

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

string1 = "He turned to me and said, \"Hello there\""
Run Code Online (Sandbox Code Playgroud)

会比说的更难看

string2 = 'He turned to me and said, "Hello there"'
Run Code Online (Sandbox Code Playgroud)

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


i_a*_*orf 4

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

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