Python Learning (2) -- difference between single quotes, double quotes, and three double quotes

Source: Internet
Author: User

Python single quotes, double quotes, and three double quotes

Python strings usually have single quotes ('... '), double quotation marks ("... "), three quotation marks ("""... ") or ('''... '''). A string enclosed by three quotation marks can be composed of multiple rows. Generally, it can represent a narrative string of a large segment. There is basically no difference in use, but double quotation marks and three quotation marks ("""... ") can contain single quotes, three quotes ('''... ''') can contain double quotation marks without escaping.

For example, S1 = "Hello, world" if you want to write multiple lines, you need to use the \ ("hyphen,

For example, S2 = "Hello ,\

World"

S2 is the same as S1. If you use three double quotes, you can write them directly,

For example, S3 = "" Hello,

World,

Hahaha .""",

S3 is actually "Hello, \ nworld, \ nhahaha. ", pay attention to" \ n ". Therefore, if you have many \ n strings and you do not want to use \ n in them, you can use three double quotes. You can also add comments to the string using three double quotes,

For example, 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 the same,

When a string needs to be enclosed in quotation marks, the single quotation marks and double quotation marks can be nested with each other.

Example: Print 'test "'" test "'" '--> test "test"

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

In fact, there is a reason why Python supports single quotes. Below I will compare the difference between one single quotes and one double quotes. When I use single quotes to represent a string, if you want 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 character string is expressed with'. Therefore, you must use the Escape Character \ (\, you should know the escape character ), if your string contains a lot of escape characters, it looks uncomfortable. Python also solves this problem well,

For example, S5 = "Let's go"

At this time, we can see that python knows that you use "to represent the string, so Python treats the single quotation mark in the string as a normal character, isn't it very easy. The same applies to double quotation marks. The following is an example.
S6 = 'I realy like "Python "! 'That's why both single quotes and double quotes can represent strings.

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

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.