Home  >  Article  >  Backend Development  >  The difference between python2 and 3print

The difference between python2 and 3print

silencement
silencementOriginal
2019-06-26 09:43:282978browse

The difference between python2 and 3print

Both Python2 and Python3 provide the print() method to print information, but the print between the two versions is slightly different

Mainly reflected in the following aspects :

1. In python3, print is a built-in function with multiple parameters, while in python2, print is a grammatical structure;

2. Python2 can print without parentheses: print 'hello world', Python3 needs to add brackets print("hello world")

3. In Python2, the input string required by input must be quoted. In order to avoid some behaviors that occur when reading non-string types, I had to use raw_input() instead of input()

1. In python3, maybe the developers felt a little uncomfortable that print had two identities at the same time, so they only kept the identity of the function:

print (value1, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

As can be seen from the above method prototype,

①. print can support multiple parameters and support printing multiple strings at the same time (where... represents any multiple strings);

②. sep represents what characters are used to connect multiple strings;

③. end indicates what characters to add at the end of the string. You can easily set the printing without line wrapping by pointing to this parameter. The print statement under Python2.x will wrap the string by default after outputting the string. If you do not want to wrap the string, just Just add a "," at the end of the statement. But under Python 3.x, print() becomes a built-in function, and the old method of adding "," will not work.

>>> print("python", "tab", ".com", sep='') 
pythontab.com 
>>> print("python", "tab", ".com", sep='', end='') #就可以实现打印出来不换行 
pythontab.com

Of course, you can also use parentheses to enclose variables in python2.7, which is not wrong at all:

print('this is a string') #python2.7

But python3 changes print to function not for nothing:

In python3, you can use help(print) to view its documentation, but not in python2:

>>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.

In python3, you can use output redirection more conveniently

python2 .7, you need to complete the 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 newly added parameter of python3 print. Another handy parameter is sep, for example printing an array of integers but you want to connect them with asterisks instead of spaces. In python2, you may need to write a loop to complete it. In python3, this will work:

a = [1, 2, 3, 4, 5]
print(*a, sep = &#39;*&#39;)<br>

Finally, if you want to use python3's print in python2.7, you only need to add:

before the first line of code.

from __future__ import print_function

Note that statements such as from __future__ import... must be placed at the beginning of the code.

The above is the detailed content of The difference between python2 and 3print. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn