Summary of print differences between python2 and python3
- 2020-07-21 08:42:58
Both Python2 and Python3 provide the print() method for printing information, but print differs slightly between the two versions
Mainly reflected in the following aspects:
1. print in python3 is a built-in function with multiple parameters, while print in python2 is a syntax structure;
2.Python2 can print without parenthesis: print 'hello world', Python3 with parenthesis print("hello world")
3. In Python2, input requires the input string to be quoted. To avoid the behavior of reading a non-string type, raw_input() has to be used instead of input().
1. In python3, maybe the developers are a little annoyed that print has two identities at the same time, so they just keep the identity of the function:
print(value1, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
As you can see from the method prototype above,
. print can support multiple parameters, support the simultaneous printing of multiple strings (where... Represents any number of strings);
. sep means what character connection is used between multiple strings;
The print statement under Python2.x will default to a newline after the output string. If you do not want a newline, just add 1 ", "at the end of the statement. But with Python 3.x, print() becomes a built-in function and the old ", "method doesn't work.
>>> print("python", "tab", ".com", sep='') pythontab.com >>> print("python", "tab", ".com", sep='', end='') # You can print it out without wrapping pythontab.com
Python2 can be printed without parentheses: print 'hello world', Python3 with parentheses print("hello world")
print in python3 must use parentheses because it is just a function.
In general, the print of Python2.7 is not an function, but an print in Python3 is an function.
The main differences between the two ways of calling are as follows:
print 'this is a string' #python2.7 print('this is a string') #python3
Of course, in python2.7 you can also use parentheses to enclose variables. One point is not wrong:
print('this is a string') #python2.7
But python3 changing print to function is not given for nothing:
1. In python3, you can use help(print) to view its documents, but python2 cannot:
>>help(print) Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
2. In python3, output redirection is easier to use
In python2.7, you need to do the redirection in a style similar to C++ :
with open('print.txt', 'w') as f: print >> f, 'hello, python!'
with open('print.txt', 'w') as f: print('hello, python!', file = f)
file is a new parameter added by python3 print. Another very handy argument is sep, such as printing an array of integers, but you want to connect with asterisks instead of Spaces. python2 may need to write a loop to complete, python3 will do:
a = [1, 2, 3, 4, 5] print(*a, sep = '*')
Finally, if you want to use print of python3 in python2.7, just add it before the first sentence:
from __future__ import print_function
___, from/future__... Statement 1 of class 1 must be placed at the beginning of the code.
print output difference: same as 1 piece of code
#/usr/bin/env python #coding:utf-8 for i in range(1,10): for j in range(1,10): for k in range(1,10): if(i != k)and(i != j)and(k != j): print(i,j,k)
The output of pyhon2 is i, j and k
The output of python3 is i j k
The output of python3 directly blocks commas.
In addition, the print sequence of python2 does not need parentheses.
phthon3 must have parentheses.