What is the difference between print and print() in python 2.7
Question:
I am newbie on Python.
I run the following code on python 2.7 and I see different result when I use print or print(). What is the difference between these two functions?
I read other questions e.g., this question, but I didn’t find my answer.
class Rectangle:
def __init__(self, w, h):
self.width = w
self.height = h
def __str__(self):
return "(The width is: {0}, and the height is: {1})".format(self.width, self.height)
box = Rectangle(100, 200)
print ("box: ", box)
print "box: ", box
The result is:
('box: ', <__main__.Rectangle instance at 0x0293BDC8>)
box: (The width is: 100, and the height is: 200)
Answers:
In the first example you’re printing tuple, but not calling print function.
So the following is identical:
a = ("box: ", box)
print a
In other words, in first example you’ve created a tuple and printed it. Different output, for different datatypes.
There’s supposed to be no significant difference between function and statement for your case, but for sake of future I’m strongly advice you to always use function (print()
). However, if you’re still interested in differences (not related to your case), with print function you can specify separator, end and where to output it as described in documentation.
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.
This in mainly a complement to other answers.
You can see in Python 2 scripts print (var)
when the normal usage would be print var
.
It uses the fact that (var)
is just a parenthesed expression in Python 2 with is simply seen as var
so print(var)
and print var
behaves exactly the same in Python 2 – but only works when printing one single variable
The interesting point is that when you considere a migration to Python 3, print(var)
(here the call to function print) is already the correct syntax.
TL/DR: print(var)
in Python 2 is just a trick to ease Python3 migration using the fact that (var)
is just an expression – the tuple form would be (var,)
In python 2.7 print() is faster than print. Here a test make with Repl.it Python Console Online :
import time
start_time = time.time()
print "lol"
end_time = time.time()
print(end_time - start_time)
start_time_2 = time.time()
print ("lol")
end_time_2 = time.time()
print(end_time_2 - start_time_2)
print((end_time_2 - start_time_2) > (end_time - start_time))
Python 2.7.10
[GCC 4.8.2] on linux
lol
7.08103179932e-05
lol
1.00135803223e-05
False
In Python 2.7: print is not a function, it is a keyword and acts as a special statement.
Before that, we need to learn more about tuple and expr in python2.
If we write (‘hello’): it is regarded as an expression, not a tuple, it can be only be regarded as a tuple if “,” is introduced.
eg:
print("hello")
>>> hello
a=("hello")
>>>'hello'
a=("hello", "world")
>>>('hello', "world")
print("hello", "world")
>>> ('hello', 'world')
so in python2.7, when we use print statement with parenthesis, it is still regarded as a statement not a python function and values with parenthesis are treated to be a tuple or an expression while in python 3, print is no longer a statement it is considered to be a function.
In order to use the print function of python 3 in python2 use the following import statement,
from __future__ import print_function
I am newbie on Python.
I run the following code on python 2.7 and I see different result when I use print or print(). What is the difference between these two functions?
I read other questions e.g., this question, but I didn’t find my answer.
class Rectangle:
def __init__(self, w, h):
self.width = w
self.height = h
def __str__(self):
return "(The width is: {0}, and the height is: {1})".format(self.width, self.height)
box = Rectangle(100, 200)
print ("box: ", box)
print "box: ", box
The result is:
('box: ', <__main__.Rectangle instance at 0x0293BDC8>)
box: (The width is: 100, and the height is: 200)
In the first example you’re printing tuple, but not calling print function.
So the following is identical:
a = ("box: ", box)
print a
In other words, in first example you’ve created a tuple and printed it. Different output, for different datatypes.
There’s supposed to be no significant difference between function and statement for your case, but for sake of future I’m strongly advice you to always use function (print()
). However, if you’re still interested in differences (not related to your case), with print function you can specify separator, end and where to output it as described in documentation.
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.
This in mainly a complement to other answers.
You can see in Python 2 scripts print (var)
when the normal usage would be print var
.
It uses the fact that (var)
is just a parenthesed expression in Python 2 with is simply seen as var
so print(var)
and print var
behaves exactly the same in Python 2 – but only works when printing one single variable
The interesting point is that when you considere a migration to Python 3, print(var)
(here the call to function print) is already the correct syntax.
TL/DR: print(var)
in Python 2 is just a trick to ease Python3 migration using the fact that (var)
is just an expression – the tuple form would be (var,)
In python 2.7 print() is faster than print. Here a test make with Repl.it Python Console Online :
import time
start_time = time.time()
print "lol"
end_time = time.time()
print(end_time - start_time)
start_time_2 = time.time()
print ("lol")
end_time_2 = time.time()
print(end_time_2 - start_time_2)
print((end_time_2 - start_time_2) > (end_time - start_time))
Python 2.7.10
[GCC 4.8.2] on linux
lol
7.08103179932e-05
lol
1.00135803223e-05
False
In Python 2.7: print is not a function, it is a keyword and acts as a special statement.
Before that, we need to learn more about tuple and expr in python2.
If we write (‘hello’): it is regarded as an expression, not a tuple, it can be only be regarded as a tuple if “,” is introduced.
eg:
print("hello")
>>> hello
a=("hello")
>>>'hello'
a=("hello", "world")
>>>('hello', "world")
print("hello", "world")
>>> ('hello', 'world')
so in python2.7, when we use print statement with parenthesis, it is still regarded as a statement not a python function and values with parenthesis are treated to be a tuple or an expression while in python 3, print is no longer a statement it is considered to be a function.
In order to use the print function of python 3 in python2 use the following import statement,
from __future__ import print_function