I want to explain the print of Python and learn pythonprint.
Eval ()
Let's take a look at this stuff before we print it. It is not useless, because it may be used in some cases.
Copy codeThe Code is as follows:
>>> Help (eval) # This is a rare concept. If you don't understand how to use it, use this 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 be any mapping,
Defaulting to the current globals and locals.
If only globals is given, locals defaults to it.
It is better to understand and understand. See what I wrote. Haha. To sum up, eval () is used to calculate a string that matches a python expression. Meaning:
Copy codeThe Code is as follows:
>>> 3 + 4 # This is an expression. python calculates the result according to the calculation rules.
7
>>> "3 + 4" # This is a string. python does not calculate the content, although it is an expression conforming to the python specifications.
'3 + 4'
>>> Eval ("3 + 4") # This is different from the above. The expression in the string is calculated.
7
The following is an example of adding a string:
Copy codeThe Code is as follows:
>>> "Qiwsir" + ". github. io"
'Qiwsir. github. io'
>>> "'Qiwsir '+'. github. io '" # In the string, python does not perform "computing ".
"'Qiwsir '+'. github. io '"
>>> Eval ("'qiwsir '+'. github. io '") # eval () does different things. It calculates the strings.
'Qiwsir. github. io'
By the way, another function that is a bit similar to eval (): exec () is used to execute python statements in strings or files.
Copy codeThe Code is as follows:
>>> Exec "print 'hello, qiwsir '"
Hello, qiwsir
>>> "Print 'hello, qiwsir '"
"Print 'hello, qiwsir '"
Print details
The print command is used in many programming practices. In particular, you need to use print to output the program running at a certain time, or in a broader sense, it is necessary to explain the problem of output results in the program.
Relatively simple output, as mentioned earlier:
Copy codeThe Code is 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
Here, % s and % d are placeholders.
Copy codeThe Code is 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 # output decimal places as required
3.14
>>> Print "%. 9f" % a # if the number of decimal places required is too large, add 0 to the end.
3.141592600
>>> B = 3
>>> Print "% 4d" % B # if it is an integer, this write requires that the integer occupies four positions, so three spaces are added before
3 # instead of writing a 0003 Style
In this way, it is a little different from the above.
Copy codeThe Code is as follows:
>>> Import math # introduce the Mathematical Module
>>> Print "PI = % f" % math. pi # by default, the circumference rate is printed as this
PI = 1, 3.141593
>>> Print "PI = % 10.3f" % math. pi # constraints. This meaning is that the integer plus the decimal point and the decimal part total 10 digits, and the right alignment
PI = 1, 3.142
>>> Print "PI = %-10.3f" % math. pi # requires the left alignment of the display, and the rest are the same as above
PI = 1, 3.142
>>> Print "PI = % 06d" % int (math. pi) # display of the integer part. A total of six digits are required, so that the first digit is supplemented with 0.
PI = 1, 000003
In fact, similar to the preceding numeric operation, you can also perform some constrained output operations on strings. Take a look at the following experiment. It is best to check the official website.
Copy codeThe Code is as follows:
>>> Website
'Qiwsir. github. io'
>>> Print "%. 3 s" % website
Qiw
>>> Print "%. * s" % (3, website)
Qiw
>>> Print "% 7.3 s" % website
Qiw
>>> Print "%-7.3 s" % website
Qiw
In general, it is similar to the number output operation. However, in actual operations, these operations are actually not much. At least during my many years of code career, I used the complex operations above, that is, when I present the column spaces, at best, we use the float type data output decimal point operation. Other output operations are mostly in the default mode. Please take it easy to despise my ignorance here.
At this point, the reminder column is used. If python3 is used, use print () to add parentheses.
Print has a feature, that is, when output, each line is automatically followed by a line break \ n, which has been mentioned earlier.
Copy codeThe Code is as follows:
>>> Website
'Qiwsir. github. io'
>>> For word in website. split ("."):
... Print word
...
Qiwsir
Github
Io
>>> For word in website. split ("."):
... Print word, # Note: Add a comma to change the output format.
...
Qiwsir github io
% R is omnipotent?
I once said that lazy people change the world, especially in coding. As a result, someone asked, "% s" and "% d" are there any problems. Is there 10 thousand? So someone on the Internet gave the answer, and % r is omnipotent. Test:
Copy codeThe Code is as follows:
>>> Import math
>>> Print "PI = % r" % math. pi
PI = 1, 3.141592653589793
>>> Print "Pi = % r" % int (math. pi)
Pi = 3
It's really omnipotent! Don't worry. Look at this. Are you confused?
Copy codeThe Code is as follows:
>>> Print "Pi = % s" % int (math. pi)
Pi = 3
Of course, an error will surely occur:
Copy codeThe Code is 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 you see this, it would be quite normal to look at the official website, especially the so-called omnipotent % r and % s, how can we make a normal output of the original % d?
In fact, both % r and % s (% d) convert the object as an integer into a string output instead of an output integer. However, % r is a little different from % s. I will not conduct any further research on this topic, but it just shows the corresponding: % s --> str (); % r --> repr (), what does it mean? That is to say, % s calls the str () function to convert the object to the str type, while % r calls repr () to convert the object to a string. For the differences between the two, see Difference between str and repr in Python. The following is a simple example to illustrate the differences between the two:
Copy codeThe Code is as follows:
>>> Import datetime
>>> Today = datetime. date. today ()
>>> Today
Datetime. date (2014, 8, 15)
>>> Str (today)
'2017-08-15'
>>> Repr (today)
'Datetime. date (2014, 8, 15 )'
Finally, I want to express my point of view. There is nothing to do. Everything depends on actual needs.
For more information about the placeholders in the output format, there is a table on this page. Unfortunately, Chinese characters are not found. If the viewer finds a Chinese character, please share it with us: string formatting.
Extension
Copy codeThe Code is as follows:
>>> Myinfo
{'Website': 'qiwsir. github. io ', 'name': 'qiwsir', 'room': 703}
>>> Print "qiwsir is in % (room) d" % myinfo
Qiwsir is in 703
Do you understand the output above? A little interesting. This output is an extension of the previous output.
In addition to this extension, you can also use the name "format" when outputting the file. Here, % is not displayed, but {} is added {}. Take a look at the experiment first:
Copy codeThe Code is as follows:
>>> Print "My name is {0} and I am in {1}". format ("qiwsir", 703) # Fill in the Content following the format
My name is qiwsir and I am in 703
>>> "My website is {website}". format (website = "qiwsir. github. io") # {} is equivalent to a variable.
'My website is qiwsir. github. io'
Do you think this format is interesting? Do not lose to the previous output mode at all. It is said that the format will gradually replace the previous one. For the format, I plan to continue later. Here is just an introduction, and the output in format will be more detailed later.
Python print Problems
The first problem is the version. You should use python3.0, and 3.0 is the version just released. The latest version is 2.5, so an error will be reported, you can simply use version 2.5 or below, and read the common usage of print.
The second problem is that raw_input assigns your input value to the variable, for example, your
A = raw_input ("please input ")
I tried to output please input a in the interactive window.
Then you should enter a character behind and press Enter. the character you entered will be assigned to.
But I don't know if python3.0 is the syntax or your Ide problems...
Print for beginners in python
In the shell of python, it is impossible to enter the multi-line input mode such as print 1 .....