python中的单引号,双引号和三引号有什么区别? [重复]

时间:2022-03-14 22:27:43

This question already has an answer here:

这个问题在这里已有答案:

In other words, how do I know which one to use?

换句话说,我怎么知道使用哪一个?

I know when I use strings, I would do

我知道当我使用琴弦时,我会这样做

    string = "This is a string"

when would I use ' ' or """ """?

我何时会使用''或“”“”“”?

1 个解决方案

#1


13  

'...' and "..." are equivalent. If you have an apostrophe in the string, it is easier to use "..." so you don't have to escape the apostrophe. If you have quotes in the string, it's easier to use '...' so you don't have to escape the quotes.

'......'和“......”是等价的。如果字符串中有撇号,则使用“...”会更容易,因此您无需转义撇号。如果您在字符串中有引号,则更容易使用'...',因此您不必转义引号。

Triple quotes (both varieties, """ and ''' are permitted) allow the string to contain line breaks. These are commonly used for docstrings (and other multi-line comments, including "commenting out" code) and for embedded snippets of other computer languages such as HTML and SQL.

三引号(允许两种变体,“”和“允许”)允许字符串包含换行符。这些通常用于文档字符串(和其他多行注释,包括“注释掉”代码)和嵌入式片段其他计算机语言,如HTML和SQL。

https://docs.python.org/2.0/ref/strings.html

#1


13  

'...' and "..." are equivalent. If you have an apostrophe in the string, it is easier to use "..." so you don't have to escape the apostrophe. If you have quotes in the string, it's easier to use '...' so you don't have to escape the quotes.

'......'和“......”是等价的。如果字符串中有撇号,则使用“...”会更容易,因此您无需转义撇号。如果您在字符串中有引号,则更容易使用'...',因此您不必转义引号。

Triple quotes (both varieties, """ and ''' are permitted) allow the string to contain line breaks. These are commonly used for docstrings (and other multi-line comments, including "commenting out" code) and for embedded snippets of other computer languages such as HTML and SQL.

三引号(允许两种变体,“”和“允许”)允许字符串包含换行符。这些通常用于文档字符串(和其他多行注释,包括“注释掉”代码)和嵌入式片段其他计算机语言,如HTML和SQL。

https://docs.python.org/2.0/ref/strings.html