With the old Ziko python print detailed _python

Source: Internet
Author: User

Eval ()

Before you do anything with print, look at this stuff first. It's not useless, because it might be used at some point.

Copy Code code as follows:

>>> Help (eval) #这个是一招鲜, when you don't understand how to use it, look at the document.

Help on built-in function eval in module __builtin__:

Eval (...)
Eval (source[, globals[, locals]])-> value

Evaluate the source in the context of globals and locals.
The source may be a string representing a Python expression
Or a code object as returned by compile ().
The globals must be a dictionary and locals can is any mapping,
Defaulting to the current globals and locals.
If only globals are given, locals defaults to it.

It's better to see and understand, and it doesn't matter if you don't understand. Look what I wrote. Ha ha. To sum up, eval () computes the object that matches the Python expression in the string. The meaning is:

Copy Code code as follows:

>>> 3+4 #这是一个表达式, Python calculates the results by calculating the rules
7
>>> "3+4" #这是一个字符串, Python does not compute the contents, although it is an expression that conforms to the Python specification
' 3+4 '
>>> eval ("3+4") #这里就跟上面不一样了, the expression inside the string is computed.
7

Let's look at the example of a string "add":

Copy Code code as follows:

>>> "Qiwsir" + ". Github.io"
' Qiwsir.github.io '
>>> "' Qiwsir ' + '. Github.io '" #字符串里面, Python is not going to "compute"
"' Qiwsir ' + '. Github.io '"
>>> eval ("' Qiwsir ' + '. Github.io '") #eval () doing something completely different, it will calculate the inside of the string
' Qiwsir.github.io '

By the way, another function that is similar to eval () is exec (), which is dedicated to executing the python statements in a string or file.

Copy Code code as follows:

>>> exec "print ' Hello, Qiwsir '"
Hello, Qiwsir.
>>> "print ' Hello, Qiwsir '"
"Print ' Hello, Qiwsir '"

Print Detailed

The Print command is used more in programming practice, especially to see what happens to a program at some point in time, must print to output, or, to say more broadly, to understand the results of the program to output the problem.

Relatively simple output, which has been covered before:

Copy Code code as follows:

>>> name = ' Qiwsir '
>>> room = 703
>>> website = ' Qiwsir.github.io '
>>> print "My name is:%s\nmy room is:%d\nmy website is:%s"% (name,room,website)
My name Is:qiwsir
My room is:703
My website Is:qiwsir.github.io

Where%s,%d is a placeholder.

Copy Code code as follows:

>>> A = 3.1415926
>>> print '%d '%a #%d can only output integers, int type
3
>>> print "%f"%a #%f output floating-point number
3.141593
>>> print "%.2f"%a #按照要求输出小数位数
3.14
>>> print "%.9f"%a #如果要求的小数位数过多, with 0 full complement
3.141592600
>>> B = 3
>>> print "%4d"%b #如果是整数, this write requires that the integer occupies four positions, and then add three spaces in front
3 #而不是写成0003的样式

Another paradigm, written like this, is a little different from the above.

Copy Code code as follows:

>>> Import Math #引入数学模块
>>> print "pi=%f"%math.pi #默认, printing pi as this
pi=3.141593
>>> print "pi=%10.3f"%math.pi #约束一下, this means that the integer part plus decimal and decimal parts total 10 bits, and the right alignment
Pi= 3.142
>>> print "pi=%-10.3f"%math.pi #要求显示的左对齐, the rest is the same as above
pi=3.142
>>> print "pi=%06d"%int (Math.PI) #整数部分的显示, requiring a total of 6, so the front with 0 to make up.
pi=000003

In fact, similar to the above digital operation, the string can also do some constrained output operations. Look at the following experiment, the best reader also try.

Copy Code code as follows:

>>> website
' Qiwsir.github.io '
>>> print "%.3s"%website
Qiw
>>> print "%.*s"% (3,website)
Qiw
>>> print "%7.3s"%website
Qiw
>>> print "%-7.3s"%website
Qiw

In general, it is similar to the output operation of a number. However, in the actual operation, these use really not a lot of, at least in my years of code life, with the above complex operations, is now to show you, at best, with a float type of data output decimal number of operations, other output operations, by default that way mostly. Please reader here despise my ignorance.

To do this, remind all of you, if you use Python3, use Print (), to add parentheses.

One feature of print is that it automatically adds a newline symbol after each line, which is mentioned earlier.

Copy Code code as follows:

>>> website
' Qiwsir.github.io '
>>> for Word in Website.split ("."):
... print word
...
Qiwsir
GitHub
Io
>>> for Word in Website.split ("."):
... print word, #注意, with a comma, the output is changed.
...
Qiwsir GitHub IO

Is%r a panacea?

I have said that lazy people change the world, especially in the field of knocking code. So someone asked, the front is%s, for a moment is%d, trouble, is there a panacea? So someone on the internet to give the answer,%r is omnipotent. See Experiment:

Copy Code code as follows:

>>> Import Math
>>> print "pi=%r"%math.pi
pi=3.141592653589793
>>> print "pi=%r"%int (Math.PI)
Pi=3

It's the almighty! Don't worry, look at this, are you confused?

Copy Code code as follows:

>>> print "pi=%s"%int (Math.PI)
Pi=3

Of course, there must be a mistake:

Copy Code code as follows:

>>> print "p=%d"% "PI"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError:%d format:a number is required, not str

If see here, reader a bit confused is very normal, especially the so-called omnipotent%r and%s, how can originally belong to%d normal output?

In fact, either%r or%s (%d) converts an object that is an integer into a string output rather than an integer. But%r and%s are a little different, this talk about this is not done in-depth study, just to illustrate the corresponding:%s-->str ();%r-->repr (), what does it mean? This means that%s invokes the STR () function to convert the object to the STR type, while%r calls Repr () to convert the object to a string. Please refer to the difference between the two: difference between STR and repr in Python, here is a simple example to illustrate the difference between the two:

Copy Code code as follows:

>>> Import datetime
>>> today = Datetime.date.today ()
>>> today
Datetime.date (2014, 8, 15)
>>> Str (today)
' 2014-08-15 '
>>> repr (today)
' Datetime.date (2014, 8, 15) '

Finally, to express my point of view, there is nothing omnipotent, everything is based on the actual needs.

About more output format placeholder description, this page has a table, unfortunately did not find Chinese, if reader find Chinese, please share AH: string formatting

and then expand

Copy Code code as follows:

>>> MyInfo
{' website ': ' Qiwsir.github.io ', ' name ': ' Qiwsir ', ' Room ': 703}
>>> print "Qiwsir is in% (room) d"%myinfo
Qiwsir is in 703

Does reader see the output above? It's kind of interesting. This output is an extension of the previous output.

Out of this extension, in the output, you can also use a name: Format of things, here is not see%, but more {}. Look at the experiment first:

Copy Code code as follows:

>>> print "My name are {0} and I am in {1}". Format ("Qiwsir", 703) #将format后面的内容以此填充
My name are Qiwsir and I am in 703
>>> ' My website is {website} '. Format (website= "Qiwsir.github.io") #{} that's the equivalent of a variable.
' My website is Qiwsir.github.io '

See here, do you feel this format is a bit interesting? is not lost to the previous output mode. It is said that format will gradually replace the front. As for format, I plan to go on to the later. Here is just a primer, followed by the format output to make more points.

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.