Basic Python Tutorial (ii)

Source: Internet
Author: User
Tags escape quotes

Continue with the first piece of content, explaining some of the basic things about Python.

Comments

To make it easier for others to understand the program, it is very effective to use annotations, even if you look back at the old code.

# get username:>>> user_name = raw_input ("What'syour name?")   

Use the pound sign (#) in Python toindicate acomment. the memory on the right of the pound sign (#) will not be executed by the program. Even without annotations, you should make the code itself easy to understand. Fortunately! Python is an excellent language that helps programmers write easy-to-understand programs. ^_^

String

Single-quote strings and escape quotes

"hello,world"'hello,world'"Hello, world" she said"" Hello, world "she said  "' Hello,world ' she said '"' Hello,world ' she said"      

The first output, obviously double quotation marks (""), how the input becomes a single quotation mark ("). What difference does it have? In fact, there is no difference.

Second, the single-Boot (') contains a pair of double quotation marks (""), this time the complete output. Is it only the double quote output that turns into single quotes?

Third, the double quotation mark ("") contains a pair of single quotation marks ("), this time why not the outermost double quotes into single quote output? This compiler is more emotional, hehe!

"Let's Go" "Let's Go">>>'Let's go ' let\ 's Go ' " Let ' s go"              

The first output, the double quotation mark contains a single quotation mark (actually should be called the apostrophe (') , because it is not in pairs appear. ) can be output normally.

The second output, a pair of single quotes containing a single quotation mark (which should be the intention of the input). The compiler doesn't know how to recognize it.

The third output, for this purpose, we need to escape the middle single quotation mark with a slash (\). This time again, the single quote output turns into double quotes. compiler, you're so naughty.

Stitching strings

Try the plus sign (+) below to stitch a character

"Helle," +"world!" 'helle,world!'

>>> x="Hello," >>> y="world!" >>> x+y'hello,world!'

String representation,str and repr

In the previous example, the reader may have noticed that all strings printed by Python are enclosed in quotation marks. This is because python prints the value in the Python code, rather than the state you want the user to see. If you use the print statement, the result is different:

"hello,world! "'hello,world!" hello,world! theprint 10000l10000       

As you can see, the long integer number 10000L is converted to a digital 10000 , and also when displayed to the user.

What we're talking about here is actually two mechanisms of value being converted to characters. You can use these two mechanisms with the following two functions:

Print str ("hello,world!" Print str (10000Lprint repr ("hello,world!" )'hello,world!  Print repr (10000L) 10000L            

The STR () function, which converts the value into a reasonable form of a string, as the user can understand;

The repr () function, which creates a string that represents a value in the form of a valid Python expression.

Comparison of input and Raw_input

The last example of the previous chapter uses the raw_input function, what does it have to do with input ? Let's use the input function and try that example again.

>>> name = input ("What is your name?") WhatIsYour Name?huhutraceback (most recent): File"<pyshell#0>", Line 1,In <module>Name = input ("What is your name?") File"<string>", Line 1,In <module>nameerror:name  "huhu< Span style= "COLOR: #800000" > ' is not defined>>> name = input (" what is name?< Span style= "COLOR: #800000" > ") what is name?" huhu ">>> print " hello,  "!" hello, Huhu!            

The input () function assumes that the user entered a valid python expression. So the direct input Huhu system will prompt the error, but if the quotation marks ("huhu") will be a valid character, the program run is not a problem.

However, requiring users to enter their names with quotes is a bit excessive, so this requires the use of the raw_input function.

>>> input ("Enter a namber:") Enter a namber:33>>> raw_input ("Enter a namber: ") Enter a namber:3'3'          

Of course , input has special needs, such as requiring the user to enter a number.

Long String

If you need to write a very very long string, it needs to span multiple lines, then you can use three quotation marks with a dot of ordinary quotation marks.

"This is a very long string. It continues Here.and it ' s not over yet. "is a very long string. It continues here. and it's not over yet.      

Normal strings can also span rows. If the last character in a row is a backslash, the line break itself is "escaped", which is ignored.

" hello.world! " hello.world!>>> 1+2+ 4+512   

Raw string

>>> path ='C:\abcprint pathc:bc   

How can this be, I want to enter a path, but is changed line.

'c:\\abc'c:\\program Files\\fnord\\foo\\bar\\baz\\frozz'C:\Program files\fnord\foo\bar\ Baz\frozz     

The path problem is solved by the above double slash (\ \), but it will be too much trouble if the path is long.

Print R'C:\Program files\fnord\foo\bar\baz\frozz'print R'let\ ' s go!' let\'s go!         

The original string does not treat the backslash as a special character. As you can see, the original string starts with R .

Unicode String

The last type of string constant isUnicodeunicode < Span style= "font-family: Arial" > object --- python8 bit ascii yards form the storage, and unicode strings are stored as 16 bit unicode character, so that it can represent more character sets, including the special characters of most languages in the world.  

If you're not short Jane what is Unicode , you can access the Unicode Web site: http://www.unicode.org

>>> u'Hello, world!' u'Hello, world!       

As you can see, theUnicode string uses the U prefix, just as the original string uses R .

Note: In python 3.0 , all strings are Unicode strings.

Basic Python Tutorial (ii)

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.