Using Print() (The Function Version) in Python2.X

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

Consider the following expressions:

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

They're 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!")

You'll 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, it's 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 in python 2.7

Are you using the print_function future import?

from __future__ import print_function

That function backports the new print syntax to Python 2 code. It is usually used if a codebase should be runnable both on Python 2 and 3.

Example:

>>> print 'hello'
hello
>>> from __future__ import print_function
>>> print 'hello'
File "<stdin>", line 1
print 'hello'
^
SyntaxError: invalid syntax
>>> print('hello')
hello

See the __future__ docs for more details.

In case you're not using that import yourself, you could test whether the problem only occurs in ipython or also in regular python, to narrow down the source of the problem.

Should I use the print statement or function in Python 2.7?

Python 2.7.10 does not have a print() function, unless you import it. Adding parentheses doesn't turn it into a function, it just specifies grouping. If you try to mimic passing multiple objects, it'll print a tuple.

>>> print 1
1
>>> print (1)
1
>>> print 1,2
1 2
>>> print (1,2)
(1, 2)

Adding parentheses around what you print may make your program slightly more robust in terms of whether it's run with Python 2 or 3, but anything more than basic usage will produce unexpected results. print (1,2) in Python 3 will produce the same result as print 1,2 in Python 2. If you want actual compatibility, you should import the print function (which can be done safely in Python 2 or 3, but only makes a difference in Python 2):

>>> from __future__ import print_function
>>> print (1,2)
1 2

What is the difference between print and print() in python 2.7

In Python 2.7 (and before), print is a statement that takes a number of arguments. It prints the arguments with a space in between.

So if you do

print "box:", box

It first prints the string "box:", then a space, then whatever box prints as (the result of its __str__ function).

If you do

print ("box:", box)

You have given one argument, a tuple consisting of two elements ("box:" and the object box).

Tuples print as their representation (mostly used for debugging), so it calls the __repr__ of its elements rather than their __str__ (which should give a user-friendly message).

That's the difference you see: (The width is: 100, and the height is: 200) is the result of your box's __str__, but <__main__.Rectangle instance at 0x0293BDC8> is its __repr__.

In Python 3 and higher, print() is a normal function as any other (so print(2, 3) prints "2 3" and print 2, 3 is a syntax error). If you want to have that in Python 2.7, put

from __future__ import print_function

at the top of your source file, to make it slightly more ready for the present.

print a function by default in Python 2.7.11?

Note that the string "a function?" is an expression in Python, and an expression in parentheses is also an expression.

So your command was print ("a function?"), just printing an expression.

That is convenient for writing a line that works in both Python 2.x and in Python 3.x. The book "Python Crash Course" uses this to show code that works in both versions of Python.

Why is parenthesis in print voluntary in Python 2.7?

In Python 2.x print is actually a special statement and not a function*.

This is also why it can't be used like: lambda x: print x

Note that (expr) does not create a Tuple (it results in expr), but , does. This likely results in the confusion between print (x) and print (x, y) in Python 2.7

(1)   # 1 -- no tuple Mister!
(1,) # (1,)
(1,2) # (1, 2)
1,2 # 1 2 -- no tuple and no parenthesis :) [See below for print caveat.]

However, since print is a special syntax statement/grammar construct in Python 2.x then, without the parenthesis, it treats the ,'s in a special manner - and does not create a Tuple. This special treatment of the print statement enables it to act differently if there is a trailing , or not.

Happy coding.


*This print behavior in Python 2 can be changed to that of Python 3:

from __future__ import print_function


Related Topics



Leave a reply



Submit