Understanding of single, double, and triple quotation marks in python

One, single quotes and double quotes

In Python, we all know that single quotes and double quotes can be used to represent a string, such as

str1 = 'python'
str2 = "python" 

There is no difference between str1 and str2. But if you encounter a situation where you need to escape characters, look at the single and double quote versions.

Single quoted version:

str3 = 'We all know that \'A\' and \'B\' are two capital letters.'

Double-quoted version:

str4 = "We all know that 'A' and 'B' are two capital letters."

Single quotation marks need to be added with'\' to allow the compiler to determine that it is currently an escape character, while double quotation marks are much more convenient.

Conversely, if there are double quotation marks in the string, in order to avoid the use of escape characters, you can use single quotation marks to define the string.

str5 = 'The teacher said: "Practice makes perfect" is a very famous proverb.'

Two, 3 single quotes and 3 double quotes

In fact, 3 single quotation marks and 3 double quotation marks are not often used, but they are of great use in some special format strings. Normally, when we define a string with single or double quotes, we can only write the strings together in one line. If we have to write them in multiple lines, we have to add a \ after each line to indicate a hyphen, such as:

str1 = "List of name:\
        Hua Li\
        Chao Deng"

And even if you write like this, you can't get the expected output:

List of name: 
Hua Li 
Chao Deng

In fact, the output is as follows:

>>> str1 = "List of name:\
...         Hua Li\
...         Chao Deng"
>>> print(str1)
List of name:        Hua Li        Chao Deng

So how do we get the desired output format of one line and one name? This is what the 3 quotation marks do:

>>> str1 = """List of name:
... Hua Li
... Chao Deng
... """
>>> print(str1)
List of name:
Hua Li
Chao Deng

Although we can also add \n to the string to achieve:

>>> str1 = "List of name:\nHua Li\nChao Deng"
>>> print(str1)
List of name:
Hua Li
Chao Deng

But this looks messy when typing. So in this case, try to use 3 quotation marks. As for 3 single quotation marks or double quotation marks, it is the same. Just be aware that if the string contains single quotation marks, use double quotation marks to define it.

And there is another great effect of using 3 quotation marks: adding comments

>>> str1 = """
... List of name:
... Hua Li # LiHua
... Chao Deng # DengChao
... """
>>> print(str1)
 
List of name:
Hua Li # LiHua
Chao Deng # DengChao

In addition, multi-line comments can also be enclosed in three single quotation marks''' or three double quotation marks """, for example:

Single quoted version:

#! / usr / bin / python3 
'' ' 
This is a multi-line comments, with three single quotes 
This is a multi-line comments, with three single quotes 
This is a multi-line comments, with three single quotes 
' '' 
Print ( " Hello, World!")

Double-quoted version:

#! / usr / bin / python3 
"" " 
This is a multi-line comment, use three double quotes 
This is a multi-line comment, use three double quotes 
This is a multi-line comment, use three double quotes 
" "" 
Print ( " Hello, World!")

Guess you like

Origin blog.csdn.net/weixin_42575020/article/details/107564708