Python single quotes, double quotes, and three double quotes (reprinted)
Python 22:58:47 read 54 comments 0 font size: LargeMediumSmall subscription
First, the difference between double quotation marks (1) and three double quotation marks (3). Strings represented by double quotation marks are usually written as one line.
For example:
S1 = "Hello, world"
If you want to write multiple lines, you need to use the/("hyphen"), as shown in
S2 = "Hello ,/
World"
S2 is the same as S1. If you use three double quotes, you can write them directly, as shown below:
S3 = "" Hello,
World,
Hahaha. "", so S3 is actually "Hello,/nworld,/nhahaha.", pay attention to "/N", so,
If there are many/n strings in your string and you do not want to use/N in the string, you can use three pairs.
Quotation marks. You can also add comments to the string using three double quotes, as shown below:
S3 = "Hello, # Hoho, this is hello, which can be annotated in a string of three double quotes.
World, # Hoho, this is world
Hahaha ."""
This is the difference between three double quotes and one double quotation mark. The difference between the three double quotes and one single quotation mark is also
It is the same as this. In fact, there is a reason why Python supports single quotes. Below I will compare one single quotes and
The difference between one double quotation mark.
When I use single quotes to represent a string, to represent the string let's go, it must be like this:
S4 = 'let/'s go '. Note that no, there is a' in the string, and the string is expressed with ', so
At this time, use the Escape Character/(/, you should know the escape character). If your string contains a lot
It looks uncomfortable. Python also solves this problem well, as shown below:
S5 = "Let's go"
At this time, we can see that python knows that you use "to represent the string, so Python will put
Single quotation marks (') are processed as common characters. Is it very easy.
The same applies to double quotation marks. The following is an example.
S6 = 'I realy like "Python "! '
This is why single quotes and double quotes can both represent strings.