python – Using print() (the function version) in Python2.x

python – Using print() (the function version) in Python2.x

Consider the following expressions:

a = (Hello SO!)
a = Hello SO!

Theyre equivalent. In the same way, with a statement:

statement_keyword(foo)
statement_keyword foo

are also equivalent.

Notice that if you change your print function to:

print(Hello,SO!)

Youll notice a difference between python 2 and python 3. With python 2, the (...,...) is interpteted as a tuple since print is a statement whereas in python 3, its a function call with multiple arguments.

Therefore, to answer the question at hand, print is evaluated as a statement in python 2.x unless you from __future__ import print_function (introduced in python 2.6)

print(Hello SO!) is evaluated as the statement print (Hello SO!), where the argument to the print statement is the expression (Hello SO!).

This can make a difference if you are printing more than one value; for example print(Hello, world) will print the 2-element tuple (Hello, world) instead of the two strings Hello and world.

For compatibility with Python 3 use from __future__ import print_function:

>>> print(Hello, world)
(Hello, world)
>>> from __future__ import print_function
>>> print(Hello, world)
Hello world

python – Using print() (the function version) in Python2.x

It is still evaluated as a statement, you are simply printing (Hello SO!), which simply evaluates to Hello SO! since it is not a tuple (as mentioned by delnan).

Leave a Reply

Your email address will not be published. Required fields are marked *