Single quotation marks and double quotation marks in python.

Source: Internet
Author: User
The difference between double quotation marks (1) and three double quotation marks (3) is described first. strings expressed by double quotation marks are usually written in a line such as s1 & amp; quot; hello, world & amp; quot; If you want to write multiple rows, it is necessary to use (the line operator first describes the difference between one double quotation mark and three double quotation marks. the string represented by double quotation marks is usually written as a 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 you have many \ n strings and you do not want to use \ n in the strings, 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, we need to use the escape character \ (\, the escape character should be known), 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.

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.