Python role in single and double quotes

 

A single and double quotes

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

str1 = 'python'
str2 = "python" 

str1 and str2 is no different. But if the situation encountered need to escape characters, look at the version of single and double quotes.

Single quotes Version:

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

Double quotes Version:

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

Single quotes need to add '\' to allow the compiler to determine the current is the escape character, and double quotes a lot of convenience.

Conversely, if the string in double quotes, in order to avoid the use of the escape character, single quotes may be used to define the string.

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

Two, three, and three pairs of single quotation marks

3 actually three single and double quotation marks are not often used, but in some special format of the string there is great use. Normally when we use single or double quotes only the definition of a string string together into a single line, if you must write multiple lines, you have to add at the end of each line of a \ represents hyphen, for example:

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

And even if you can not write to get the desired output:

Name of List: 
Hua of Li 
the Chao Deng,

In fact the output is like this:

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

So how to get the name of a line we expect output format it? This is the role of the three quotes:

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

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

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

But this looks when you enter a lot of chaos. Therefore, in this case to make use of three marks, as three single quotes or double quotes are the same, only need to pay attention if the string contains a single quote must be defined using double quotes like.

And the use of three quotes and a special role is to stick: Add Comment

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

 

In addition, multi-line comments can also be used with three single quotes  ' '' or three double quotation marks  "", "comment enclose, for example:

Single quotes Version:

Copy the code
#! / 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! ")
Copy the code

Double quotes Version:

Copy the code
#! / 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! ")
Copy the code

 

Reference link: https: //www.cnblogs.com/zzdbullet/p/10025301.html

Guess you like

Origin www.cnblogs.com/xiohao/p/11261802.html