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

Source: Internet
Author: User

Ython the difference between single quotes, double quotes, and three double quotes
Python strings usually have single quotes (' ... '), double quotation marks ("..."), three quotation marks ("" "..." "") or ("..."), or ("."), surrounded by a string of three quotes that can consist of multiple lines, typically representing large segments of the narrative string. There is no difference in use, but double quotes and three quotation marks ("" "" "" "" "" "") can contain single quotes, and three quotation marks ("...") can contain double quotes without escaping

such as: S1 = "Hello,world" If you want to write more than a line, then you need to use \ ("line character") it,

such as: S2 = "hello,\

World

S2 and S1 are the same. If you use 3 double quotes, you can write directly,

such as: 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 with 3 double quotes you can also add comments to the string,

For example: 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,

When a string needs to be enclosed in quotation marks, it can be nested using single quotation marks and double quotation marks.

Example: print ' Test ' ' "Test" ' "'-->> Test" test "

"Test" "' Test '" ' "-->> test ' test '

In fact Python supports single quotes for a reason, let me compare the difference between 1 single quotes and 1 double quotes. When I use single quotes 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), If you have a whole bunch of escape characters in your string, it's certainly uncomfortable, and Python solves the problem well.

Example: S5 = "Let's Go"

At this point, we see that Python knows that you are using "to represent strings, so Python will put that single quote in the string 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.


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

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.