Let’s first talk about the difference between 1 double quote and 3 double quotes. The string represented by double quotes is usually written in one line
For example:
s1 = "hello,world"
If you want to write it in multiple lines, then use ("hyphen"), such as
s2 = "hello,
world"
s2 is the same as s1. If you use 3 double quotes, you can write it directly, as follows:
s3 = """hello,
world,
hahaha.""", then s3 is actually "hello, nworld, nhahaha. ", pay attention to "n", so,
If there are a lot of n's in your string and you don't want to use n in the string, then you can use 3 double
quotes. Moreover, you can add comments to the string by using 3 double quotes, as follows:
s3 = """hello, #hoho, this is hello, you can have comments within the string of 3 double quotes
world, #hoho, this is world
hahaha."""
This is the difference between 3 double quotes and 1 double quote to represent a string. The difference between 3 double quotes and 1 single quote is also
and this Similarly, there is actually a reason why python supports single quotes. Let me compare the difference between 1 single quote and
1 double quote.
When I use single quotes to represent a string, if I want to represent the string Let's go, it must be like this:
s4 = 'Let's go', note that there is no ' in the string, and the character Strings are represented by ', so
needs to use escape characters at this time (you should know the escape characters). If your string has a lot of
escape characters, it will definitely look uncomfortable. , python also solves this problem very well, as follows:
s5 = "Let's go"
At this time, we see that python knows that you use " to represent a string, so python puts the
in the stringSingle quotation marks' are treated as ordinary characters. Isn't it very simple?
The same is true for double quotation marks. Here is an example
s6 = 'I really like "python"!'
This is why both single quotes and double quotes can represent strings