Python single quotes, double quotes, and three double quotes

Source: Internet
Author: User
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.

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: info-contact@alibabacloud.com 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.