Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!")
like image 775
Shane Smiskol Avatar asked Dec 10 '15 00:12

Shane Smiskol


People also ask

Is print a function or statement in Python?

Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

Do I need to use print in Python?

Printing and returning are completely different concepts. print is a function you call. Calling print will immediately make your program write out text for you to see. Use print when you want to show a value to a human.

Is print a function in Python 2?

Print: In Python 2, “print” is treated as a statement rather than a function. There is no need to wrap the text you want to print in parentheses, although you can if you want.

Should you have print statements in functions?

As a rule of thumb I think it's good to avoid adding print statement in functions, unless the explicit purpose of the function is to print something out. In all other cases, a function should return a value.


2 Answers

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
like image 91
TigerhawkT3 Avatar answered Oct 12 '22 23:10

TigerhawkT3


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.

like image 31
Dietrich Epp Avatar answered Oct 12 '22 22:10

Dietrich Epp