The difference between single quotes, double quotes, and three quotes in Python

Source: Internet
Author: User


Say the difference between 1 double quotes and 3 double quotes, and the string represented by double quotes is usually written as a line
Such as:
S1 = "Hello,world"
If you want to write multiple lines, then use \ ("line character"),

s2 = "hello,\
World"
S2 is the same as S1. If you use 3 double quotation marks, you can write directly, as follows:
S3 = "" Hello,
World,
hahaha. ""

So 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. and use 3 double quotes to add comments to the string.

As follows:


world,           #hoho, this is world
hahaha. ""
This is the difference between 3 double quotation marks and 1 double quotation marks, the difference between 3 double quotes and 1 single quotes is the same as this, and actually Python supports single quotes for a reason, so let me compare 1 single quotes with 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 represent, so this time to use the escape character \ (\, the escape character should know it), if your string has a large list of escape characters, it seems certainly uncomfortable, Python is also a good solution to the problem, as follows:
S5 = "Let ' s Go"
At this point, we see that Python knows that you are using "to represent strings,

So Python just treats the single quote in the string as a normal character, isn't it 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.


In fact, in specific applications, there are many other special applications of quotation marks. Take a look at the following example:


A: A document description string used as a function


def functionname (params):

"Instructions for use, etc."

function contents

Return


If it is a document description string for a multi-line representation of a function, it is appropriate to use the following expression:


def functionname (params):

"" Use

Description

Wait a minute

"""

function contents

Return



This article is from the "Green Peak" blog, please make sure to keep this source http://zhuxianzhong.blog.51cto.com/157061/1604924

The difference between single quotes, double quotes, and three quotes in Python

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.