Python single quotes, double quotes, 3 single quotes and 3 double quotation marks the difference "go"

Source: Internet
Author: User

Single and double quotation marks

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

    1. str1 = ' python '
    2. str2 = "Python"

There is no difference between str1 and str2.

We know that Python is famous for its ease of use, so just beginning to see the tutorial learn to see both single and double quotes can be used to think that this is python in order to facilitate the programmer, whichever is good, do not worry about using the wrong. In fact, the reason behind it is not just so simple. For example, think of I ' m a big fans of Python. How this string should be defined.

Single-quote version:

    1. STR3 = ' I\ ' m a big fan of Python. '

It can be noted that there is a ' in the original string, and that Python allows the use of single quotation marks ' to represent the string, so the middle of the string ' must use the transfer character \ to be able. There is only one ' in the middle of the string, so it looks okay, but if it is We all know, then ' a ' and ' B ' are the capital letters. What about this string?

    1. STR4 = ' We all know ', ' a\ ' and ' b\ ' are both capital letters. '

What, does it look bad, and it's easy to make mistakes? This is the time when double quotes can also indicate the function of the string. Here is the double-quote version of STR4:

    1. str4_ = "We all know that ' A ' and ' B ' is the capital letters."

Does that look more humane? Yes, that's why Python supports both double and single quotes to define a string.

Conversely, if you have double quotes in a string, you can use single quotes to define the string in order to avoid using escape characters. Like what:

    1. 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 single quotation marks, it will assume that the double quote "" in your string is a normal character, so you don't need to escape. Conversely, when you define a string in double quotation marks, you will think that the single quotation marks inside your string are ordinary characters without escaping.

3 single quotes and 3 double quotes

In fact, 3 single quotes and 3 double quotes are not often used, but they are of great use in some special-format strings. Usually when we use single or double quotation marks to define a string, we can only string together to write a line, if you want to write more than one line, you have to add a \ After each line to indicate a hyphen, such as:

    1. STR1 = "List of name:\
    2. Hua li\
    3. Chao Deng "

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

    1. List of Name:
    2. Hua Li
    3. Chao Deng

The output is actually the following:

    1. >>> str1 = "List of name:\
    2. ... Hua li\
    3. ... Chao Deng "
    4. >>> Print (str1)
    5. List of Name:hua Li Chao Deng

So how do we get the output format of a name we expect from a row? This is the effect of 3 quotes:

    1. >>> str1 = "" " List of Name:
    2. ... Hua Li
    3. ... Chao Deng
    4. ... """
    5. >>> Print (str1)
    6. List of Name:
    7. Hua Li
    8. Chao Deng

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

>>> str1 ="List of Name:\nhua Li\nchao Deng"
    1. >>> Print (str1)
    2. List of Name:
    3. Hua Li
    4. Chao Deng

But that's a lot of confusion when it comes to typing, isn't it? So in this case try to use 3 quotation marks, as for 3 single quotes or double quotation marks are the same, only need to note that if the string contains a single quotation mark to use double quotation marks to define it.

And the use of 3 quotation marks has a particularly good effect is: add comments!

    1. >>> str1 = "" "
    2. ... List of Name:
    3. ... Hua Li # Lihua
    4. ... Chao Deng # Dengchao
    5. ... """
    6. >>> Print (str1)
    7. List of Name:
    8. Hua Li # Lihua
    9. Chao Deng # Dengchao

If this output is to be achieved, is it possible to use only single or double quotation marks?

Python single quotes, double quotes, 3 single quotes and 3 double quotation marks the difference "go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.