The output statements in python2.x and 3.x are significantly different
Print in 2.x is not a function, the output format is as follows
1Python 2.7.12+ (default, 4 2016, 20:04:34) 2[GCC 6.1.1 20160724] on linux23Type" Help","Copyright","credits" or "License" forMore information.4>>>Print "There is only%d%s in the sky."% (1,'Sun')5There isOnly 1 SuninchThe sky.
Print in 3.x is a function, the output format is as follows
1Python 3.5.2+ (default, 5 2016, 08:07:14) 2[GCC 6.1.1 20160724] on Linux3Type" Help","Copyright","credits" or "License" forMore information.4>>>Print("There is only%d%s in the sky."% (1,'Sun'))5There isOnly 1 SuninchThe sky.
The main reasons why such changes should be made are as follows:
1.print is not a function, can not use Help (), the user is not convenient.
Help (print) in Python2 will error.
1 >>> Help (print)2 "<stdin>", Line 13help (print)4 ^5 syntaxerror:invalid syntax
In Python3, you can use Help (print) to see the print parameters clearly.
1Help on built-inchfunctionPrint inchModule Builtins:2 3 Print(...)4 Print(Value, ..., sep=' ', end='\ n', File=sys.stdout, flush=False)5 6Prints the values to a stream,orTo sys.stdout by default.7 Optional keyword arguments:8File:a file-Like object (stream), defaults to the current sys.stdout.9 sep:string inserted between values, default a space.Ten End:string appended after the last value, default a newline. One Flush:whether to forcibly flush the stream. A(END)
2. From the above help (print) we can also see two important parameters in print (), Sep and end. These two parameters make print () more than two new features, custom spacers (default spaces), and terminator (the default carriage return).
1>>>Print("123","456","789")2123 456 7893>>>Print("123","456","789", sep='-')4123-456-789
1 >>> x=10242print(t)3 4print(t,end= " End " )56print(t,end=" end\n") 7 End
3.print () redirects the output file more conveniently.
2.x requires print>> redirection output, feeling the code is confusing.
1 >>> out=open ("test.txt","w") 2 print>>out,"123"
The output file in 3.x becomes a parameter and is more convenient to use.
1 >>> out=open ("test.txt","w") 2 Print ("123", File=out)
The formatted output of the print statement in 4.python2.x is derived from the formatted output of the C language, which is appropriate for the static language of C, but it is a bit too much for Python with many advanced data structures. Python's tuples, lists, dictionaries, collections, etc. are not suitable for this structure, and most of these data structures are represented by subscripts, which are confusing to write in this structure. The print () function of the python3.x provides a somewhat similar format output function in C # (which is not known to be correct). In addition, print () is compatible with the original format output mode.
1 Print ("%s is%s. "% ('aoko','good'))2 is good.
Format () makes the output more legible.
1 Print ("{0} is {1}. ") ". Format ('aoko','good'))2 is good.
Format () supports array subscripts, making some of the data structure output in Python more convenient.
1 >>> name=["kaito", 5]2print(" {0[0]} has {0[1]} dollars. " . Format (name)) 3 Kaito has 5 dollars.
The format qualifier under format () is similar to the original.
1 >>> x=5.62print("{0:4f}". Format (x))3 5.600000
As a result, print () has progressed much more than print. By the way, I want more Python users to spend more time implementing the code for compatibility with the new version, rather than spending time arguing about "Python2 and Python3 who better" in the war of words. Python as a free language has brought us a lot of convenience, we should spare ourselves a little time, spend a little time to let Python develop, become stronger.
"Learning Notes" Python2 print and Python3 print ()