Python single quotes, double quotes, and three double quotes differ __python

Source: Internet
Author: User

The difference between Python single quotes, double quotes, and three double quotes
Python strings usually have single quotes (' ... '), double quotation marks ("..."), three quotation marks ("" "" ""), or (' ... ') are surrounded by a string of three quotes that can be composed of multiple lines, typically representing large segments of descriptive strings. There is basically no difference when used, but double quotes and triple quotes ("" "") can contain single quotes, three quotes (' ... ') that can contain double quotes without escaping

such as: S1 = "Hello,world" If you want to write multiple lines, then use the \ ("line character"),

such as: S2 = "hello,\
World

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

such as: s3 = "" "Hello,

World

hahaha. "" ",

So S3 is actually "Hello,\nworld,\nhahaha.", notice "\ n", so if you have a lot of strings and you don't want to use \ nthe string, then you can have 3 double quotes. and use 3 double quotes to add a comment to the string.

such as: s3 = "" "Hello, #hoho, this is Hello, can be commented within the 3 double quote strings OH
World, #hoho.
hahaha. "" "

This is the difference between 3 double quotes and 1 double quotes to denote a string, and the difference between 3 double quotes and 1 single quotes is the same as this one,

When strings need to be enclosed in quotes, they can be nested using single quotes and double quotes

For example: print ' Test ' ' Test ' ' ' '-->> test ' test '

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

There's a reason Python actually supports single quotes, and I'm going to compare the difference between 1 single quotes and 1 double quotes. When I use single quotes to represent a string, if you want to represent let's go this string, it must be this way: S4 = ' let\ ' go ', note No, there is a ' in the string, and the string is used ' to represent, so this time use escape character \ (\, escape character should know), If you have a whole bunch of escape characters in your string and it certainly doesn't look comfortable, Python solves the problem very well,

such as: S5 = "Let ' s Go"
At this point, we see that Python knows that you're using "to represent strings, so it's easy for Python to treat that single quote in a string as a normal character." For double quotes, the same is true, and here's an example
S6 = ' I realy like ' python '! ' This is why single quotes and double quotes can represent strings.



Zz:http://hi.baidu.com/tianhuimin/blog/item/0759caea5b6a83ded439c9fa.html

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.