Use single quotation marks, double quotes, and multi-quote differences in Python for detailed instructions

Source: Internet
Author: User
First, the difference between 1 double quotes and 3 double quotes, the string represented by double quotation marks is usually written as a line

Such as:

S1 = "Hello,world"

If you want to write multiple lines, then use \ ("line break"), such as

S2 = "hello,\

World

S2 and S1 are the same. If you use 3 double quotation marks, you can write directly, as follows:

S3 = "" "Hello,

World

hahaha. "", then S3 is actually "Hello,\nworld,\nhahaha.", note "\ n", so,

If you have a lot of strings in your string and you don't want to use \ n in a string, then you have 3 double

Quotes. Also, use 3 double quotation marks to add comments to the string, as follows:

S3 = "" "Hello, #hoho, this is hello, there can be comments in 3 double-quoted strings.

World, #hoho

hahaha. "" "

This is the difference between 3 double quotes and 1 double quotation marks, and the difference between 3 double quotes and 1 single quotes

Is the same as this, actually Python supports single quotes for a reason, let me compare 1 single quotes and

The difference between 1 double quotes.


When I use single quotation marks to represent a string, if I want to represent let's go this string, it must:

S4 = ' let\ ' go ', note that there is a ' in the string, and the string is used ' to denote, so

This is the time to use the escape character \ (\, escape character should know), if your string has a large heap of

The escape character, looks certainly uncomfortable, Python also solves this problem very well, as follows:

S5 = "Let ' s Go"

At this point, we see that Python knows that you are using "to represent strings, so Python will put that in the string

Single quotes ', as normal character processing, is not very simple.

The same is true for double quotes, and here's an example

S6 = ' I realy like ' python '! '


This is why both single and double quotation marks can 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.