Shane Smiskol
Shane Smiskol

Reputation: 966

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

To make my simple scripts in Python 2.7.10, should I use the print function instead of the statement to make them future proof, in case I want to test them on another computer with only Python 3, or on an online IDE? What is the best way that works best with both or the most common Python versions?

What are the differences? This is my first programming language I'm learning, so I'm just unsure. Right now, I have a fixed variable that I enter in either '2' or '3' and then an if statement in the code does a different print function and concatenate method for either versions.

--Examples--

What I am using now:

print "Hello world!"

But should I use this in both versions?

print ("Hello world!")

Upvotes: 5

Views: 4501

Answers (4)

jgritty
jgritty

Reputation: 11925

Either of those lines will work in both 2.7 and 3.x

print ("Hello world!")
print("Hello world!")

An example of a 2.7 only print statement would be like this:

print "Hello world!"

You should probably just go ahead and use the parentheses to future proof your code, and the PEP8 formatting looks like this, with the caveat below:

print("Hello world!")

with no space between the function and open paren.

This above will work fine for simple strings, however, the real solution is to import the print function from future like so:

from __future__ import print_function

This will make print a real function in python 2, similar to how it is in python 3. One place where simply surrounding things in parentheses will bite you is when you do:

print 1,2

vs.

print(1, 2)

python 2 will treat the latter as a tuple, and you will see a change in output like so:

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

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49318

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

Upvotes: 11

mhawke
mhawke

Reputation: 87084

If you really want to support both Python 2 and 3, you should use the print() function, and enforce it by using a future import by placing this at the top of your file;

from __future__ import print_function

Now, in Python 2, using print as a statement is a syntax error.

>>> print ""
  File "<stdin>", line 1
    print ""
           ^
SyntaxError: invalid syntax

You can access the more advanced functionality of the print() function, for example:

>>> print(*[1,2,3,4], sep=':', end='!')
1:2:3:4!

Upvotes: 1

Dietrich Epp
Dietrich Epp

Reputation: 213338

If you want to make your Python 2 code more like Python 3, then use a __future__ import:

from __future__ import print_function

Then just use print() the way you would in Python 3.

This is much better than just putting parentheses on the print statement, because:

x = 100
print("Results:", x)

This will print ("Results:", 100) in Python 2.x, but if you use the import or Python 3.x, it will print Results: 100 as expected.

However, if you are merely concerned about portability, the 2to3 program does a very good job of converting print statements to print() function calls.

You can also import unicode_literals, division, etc. to make other parts of your code more like Python 3. Or you could just go ahead and switch to Python 3 now.

Upvotes: 3

Related Questions