Python's Path to growth first (1) __ String Initial knowledge

Source: Internet
Author: User
Tags escape quotes first string

In many programming books, the print "hello,world!" will be Like this and a program for the start, then what is Hello,world? This is the string (also a string of characters) explained in this chapter.

Single quotes, double quotes, and escape quotes

(1) In general, ' hello,world! ' and "hello,world!" There is no difference, then why can both be used? Because in some cases, they would be useful.

650) this.width=650; "id=" Code_img_closed_9ac0c843-c17c-49e7-acfc-c532a23ab9e7 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ 9ac0c843-c17c-49e7-acfc-c532a23ab9e7 "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> "Hellow,world" 2 ' hellow,world ' 3 >>> ' Hellow,world ' 4 ' hellow,world '

View Code

(2) in the following code, the first string contains single quotes, so the whole cannot be enclosed in single quotes, and if you do, the interpreter will complain (and it is right)

650) this.width=650; "id=" Code_img_closed_964eae97-36b0-4be7-8f8f-9cbbec62d3ec "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ 964eae97-36b0-4be7-8f8f-9cbbec62d3ec "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> "Let ' s go!" 2 "Let ' s go!" 3 >>> ' Ler ' s go! ' 4 Syntaxerror:invalid syntax

View Code

(3) Although the above code, executed with double quotation marks succeeds, but we do not necessarily have to do so, this involves escaping (\), so that Python will understand that one of the single quotation marks is a character

>>> ' ler\ ' s go! '
"Ler ' s go!"

Second, string spelling

(1) if we if we want to output the character of the following example, then do we need to use a lot of (\), if we do not want to use a backslash? Here we can do this in a different way

We just wrote two strings in succession, and Python automatically connects to a string (a mechanism that doesn't use much)

650) this.width=650; "id=" Code_img_closed_9d5018d5-a7c8-4e51-950f-4ab6fdeb0b36 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ 9d5018d5-a7c8-4e51-950f-4ab6fdeb0b36 "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> "\" hellow\ ", world!" 2 ' "Hellow", world! ' 3 >>> #字符串拼接4 5 >>> ' "Hellow", world! ' 6 ' "Hellow", world! '

View Code

(2) Another way to join the string in the same line as the + number, the following is the use of variables to define the character

650) this.width=650; "id=" Code_img_closed_c171233a-bcb6-4dfe-ad87-e27b117a25e0 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ C171233a-bcb6-4dfe-ad87-e27b117a25e0 "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> "Hellow," + "world!" 2 ' hellow,world! ' 3 >>> x = "Hellow," 4 >>> y = "world!" 5 >>> x+y 6 ' hellow,world! '

View Code

Note! the string in Python is represented in the C language as a character array, and each time a string is created, it needs to open a contiguous space in memory, and once you need to modify the string, you need to make room again, and the Evil + sign will re-open up a space within each occurrence.

(3) We need to use anti-quotes when we want to connect strings to numbers, for example

650) this.width=650; "id=" code_img_closed_aa98176e-9704-471d-ac56-5d86aed36754 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ aa98176e-9704-471d-ac56-5d86aed36754 "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> AA = 2 >>> print "AAAA" +AA 3 4 Traceback (most recent call last): 5 File "<PYSHELL#51&G t; ", line 1, in <module> 6 print" AAAA "+AA 7 typeerror:cannot concatenate ' str ' and ' int ' objects 8 9 >&G t;> print "aaaa" + ' AA ' ten Aaaa22

View Code

C. String representation str and REPR

(1) in the example we found that the strings printed by Pyrhon are still enclosed in quotation marks, This is because Python retains its status in Python code when it prints values, and if you do not want to see this state we use the "Print" statement to result in a different

650) this.width=650; "Id=" code_img _closed_6ef4010b-420e-4589-ae26-87cef41bba2d "class=" code_img_closed "src=" http://images.cnblogs.com/ Outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ 6ef4010b-420e-4589-ae26-87cef41bba2d "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

 1 >>> print" Hello,world " 2 hello,world  3 >>>  "Hello,world"  4  ' Hello,world ' 5 6 >>> print  1000l 7 1000 8 >>> 1000l 9 1000l 

view code

 

(2) can see the long shaping digital 1000L at print time to convert to 1000, but when we want to know whether this value is a long shape or shape what to do, in fact, the two mechanisms of the string is STR and repr to achieve, SRT we call human friendly is also convenient human understanding, Repr for machine-friendly and convenient Python expressions Let's take a few examples below.

>>> print repr ("Hello")
' Hello '
>>> Print str ("Hello")
Hello

Watch out! in Python3.0, no anti-quotes are already in use.

Iv. comparison of input and raw_input (user interaction)

(1) We already know "Hello", "+name+"! "What does that mean, then what's the difference between raw_input and input? I didn't see the following example, this example seems reasonable, but execution is wrong.

650) this.width=650; "id=" Code_img_closed_da6156d8-3e48-4b2d-bb24-5bc452b0af65 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ Da6156d8-3e48-4b2d-bb24-5bc452b0af65 "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> name = input ("What's your Name?") 2 What's Name?xiaoyuan3 4 Traceback (most recent): 5 Fil E "<pyshell#59>", line 1, in <module> 6 name = input ("What's the name?") 7 File "<string>", line 1, in <module> 8 nameerror:name ' Xiaoyuan ' are not defined

View Code

This is because we are asked to enter the user name using quotation marks

650) this.width=650; "id=" code_img_closed_69848d7c-b081-4112-8817-5c8eecd6830e "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ 69848d7c-b081-4112-8817-5c8eecd6830e "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> name = input ("What are you name?") 2 3 What are you name? " Xiaoyuan "4 >>>

View Code

So we try to use Raw_input and cancel the Raw_input in version 3.5 .

Five, long string, raw string

(1) long string, if you need to write a very very long string, it needs to span multiple lines, then, you can use 3 quotation marks instead of ordinary quotation marks,

650) this.width=650; "id=" code_img_closed_f3602e1d-b5f3-440c-b4fe-1d1859b3eb0a "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ f3602e1d-b5f3-440c-b4fe-1d1859b3eb0a "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> print "Hi there you outside 2" for it "3 Hi there you are outside 4

View Code

For normal quotation marks, you can use \ n to break the line

650) this.width=650; "id=" code_img_closed_07dceb3e-2b79-4bbe-8224-d5f238404e3f "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ 07dceb3e-2b79-4bbe-8224-d5f238404e3f "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> print "Hi there you are outside \ n" for it "2 Hi there your is outside 3" sign for it

View Code

(2) We know, \ n can come to the line, if we want to output a path "C:\new" Such a string, what should we do!

650) this.width=650; "id=" Code_img_closed_ca465f95-811e-40e9-99fa-09bba8e73a3d "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ Ca465f95-811e-40e9-99fa-09bba8e73a3d "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> new = "C:\new" 2 3 >>> print new 4 c:5 EW

View Code

Of course we can use a backslash to turn, but when the path is very long? So here we use the original string: Start with R

650) this.width=650; "id=" code_img_closed_3835e76b-d86e-4681-beaa-57b48f8f1c68 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "/>650) this.width=650;" Id= "Code_img_opened_ 3835e76b-d86e-4681-beaa-57b48f8f1c68 "class=" code_img_opened "src=" http://images.cnblogs.com/OutliningIndicators /expandedblockstart.gif "/>

1 >>> new = R "C:\new" 2 >>> print new 3 C:\new

View Code

Six, character encoding

(1) When the Python interpreter loads the code in the. py file, the content is encoded (default Ascill) ASCII (American standard Code for information interchange, The American Standard Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be represented at most 8 bits (one byte), i.e. 2**8 = 256, so that ASCII codes can represent up to 256 symbols.

650) this.width=650; "title=" image "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M02/79/10/wKiom1aGnuyCo28eAAC56tI-Chc523.png" width= "481" height= "463"/>

(2) It is obvious that ASCII code cannot represent all kinds of words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode,unicode (Uniform code, universal Code, single code) is a character encoding used on the computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

(3) UTF-8, is the compression and optimization of Unicode encoding, he no longer use a minimum of 2 bytes, but all the characters and symbols are categorized: the contents of the ASCII code is saved with 1 bytes, the characters in Europe are saved in 2 bytes, the characters in East Asia are saved with 3 bytes ... Therefore, when the Python interpreter loads the code in the. py file, it encodes the content (default Ascill), and if it is in the code, it will be an error.

#!/usr/bin/env pythonprint "Hello Tomorrow"

File "e:/pycharm/exercise/date_1/??????? /ceshi-2.py ", Line 2
Syntaxerror:non-ascii character ' \xe4 ' in file e:/pycharm/exercise/date_1/??????? /ceshi-2.py on line 2, but no encoding declared; See http://python.org/dev/peps/pep-0263/for details

So you should tell the Python interpreter what code to execute the source code with.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

print"你好明天"

Vii. notes

When the line stares: # is annotated content

Multiline Comment: "" "Annotated Content" ""

Viii. python Internal execution process

(1) when we execute a. py file, we do this in Python internally

650) this.width=650; "title=" image "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M02/79/10/wKiom1aGnu7yOTymAADs1lXPy-E113.png" width= "481" height= "273"/>

Python's Path to growth first (1) __ String Initial knowledge

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.