Article From:https://www.cnblogs.com/python2webdata/p/9968533.html

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

str1 = 'python'
str2 = "python" 

 

str1There is no difference from str2.

We know that Python is famous for its ease of use, so when we first read the tutorial and learned that single quotation marks and double quotation marks can be used, we would think that this is Python for the convenience of programmers, just use whichever is good, don’t worry about using it wrong. In fact, the reason behind this is not just so simple. For example, think about it.I’m a big fans of Python. How should this string be defined?

 

Single quotation mark version:

 str3 = 'I\'m a big fan of Python.'

 

It can be noted that the original string has a’, and Python allows the use of single quotation marks”to represent the string, so the’In the middle of the string’ must use the transfer character\. There’s only one”in the middle of the string, which looks good, but if it’s We all knowWhat about the’A’and’B’ are two capital letters?

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

  

Well, doesn’t it look very ugly, and it’s easy to make mistakes? This is the time when double quotation marks can also indicate that strings should be reflected. Below is the double quotation mark version of str4:

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

 

Does this seem more human? Yes, that’s why Python supports both double quotation marks and single quotation marks to define strings.

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

 

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

 

This is an extreme manifestation of Python’s ease of use and humanization. When you define a string with a single quotation mark’, it will assume that the double quotation mark in your string is a common character, so that escape is not required. Conversely, when you define a string with double quotation marks, you will think that it is in your string.A single quotation mark on a face is a common character that does not need to be escaped.

 

3Single quotation marks and three double quotation marks
In fact, three single quotes and three double quotes are not often used, but they are useful in some special format strings. Usually when we define a string with single or double quotation marks, we can only write strings together as one line. If we have to write multiple lines, we have to add one after each line.\ Represents hyphens, such as:

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

And even if you write like this, you can’t get the desired 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

 

>>> print('abc"def"")
abc"def"

>>> print("abc'def'")
abc'def'

 

 

So how do we get the desired output format of one line and one name? That’s what three quotes do:

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

  

Although we can also do this by adding \n to the string:

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

 

But it looks a lot messy when typing, doesn’t it? So in this case, try to use three quotation marks, as for the three single quotation marks or double quotation marks are the same, just pay attention to the use of double quotation marks to define if the string contains single quotation marks.

And there’s another great use of three quotation marks: add notes!

 

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

 

List of name:
Hua Li # LiHua
Chao Deng # DengChao

 

If we want to achieve this output effect, can we only use single quotation marks or double quotation marks?

Thanking the author

Leave a Reply

Your email address will not be published. Required fields are marked *