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 = '*')<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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
