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