Summary of the print differences between python2 and python3, python2python3
The print () method is provided in both Python2 and Python3 to print information. However, the print between the two versions is slightly different.
It is 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. You can print 'Hello world' with no brackets during Python2 printing, and print ("hello world") with brackets in Python3 ")
3. In Python2, the input string must be enclosed by quotation marks. To avoid reading non-string-type behaviors, raw_input () must be used instead of input ()
1. In python3, developers may feel that print has two identities at the same time, so they only keep the identity of the function:
Print (value1,..., sep = '', end = '\ n', file = sys. stdout, flush = False)
As shown in the preceding method prototype,
①. Print can support multiple parameters and print multiple strings at the same time (where... represents any number of strings );
②. Sep indicates the character connection used between multiple strings;
③. End indicates the characters added to the end of the string. This parameter allows you to easily set print without line breaks. print statements in Python2.x will wrap the lines by default after the output string. If you do not want line breaks, you only need to add "," at the end of the statement. But in Python 3. x, print () becomes a built-in function, and the old method of adding "," won't work.
>>> Print ("python", "tab ",". com ", sep ='') pythontab.com >>> print ("python", "tab ",". com ", sep ='', end = '') # It can be printed without wrapping pythontab.com.
Python2 can be printed without brackets: print 'Hello world', and Python3 requires brackets print ("hello world ")
Print in python3 must use parentheses because it is a function.
In general, print in Python2.7 is not a function, while print in Python3 is a function.
The main differences between the two call methods are as follows:
print 'this is a string' #python2.7print('this is a string') #python3
Of course, you can enclose variables in brackets in python2.7. It is not wrong at all:
print('this is a string') #python2.7
However, changing print to function in python3 is not white:
1. In python3, you can use help (print) to view its documentation, but python2 won't:
>>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 more convenient.
In python2.7, you need to complete redirection in a style similar to C ++:
with open('print.txt', 'w') as f: print >> f, 'hello, python!'
In python3:
with open('print.txt', 'w') as f: print('hello, python!', file = f)
File is a new parameter of python3 print. Another very handy parameter is sep. For example, print an integer array, but you want to use asterisks instead of spaces to connect. In python2, you may need to write a loop to complete it. In python3, you can do this:
a = [1, 2, 3, 4, 5]print(*a, sep = '*')
Finally, if you want to use the print of python3 in python2.7, you only need to add the following before the first code:
From _ future _ import print_function
Note that from _ future _ import... must be placed at the beginning of the Code.
Print Output difference: Same piece of code
#/usr/bin/env python#coding:utf-8for 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, k
The output of python3 is I j k.
The output of python3 directly shields the comma.
In addition, no parentheses can be added for the post-print order of python2.
Brackets must be added to phthon3.