6

I have this line of code in python

print 'hello world'

against

print ('hello world')

can someone tell me the difference between the two?

I used it in a a simple code

var = 3
if var > 2: 
    print 'hello'

it fails for checking strictly for all values for var. But if I define the code as

var = 3
if var > 2: 
    print ('hello')

it works!

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
Trigger
  • 109
  • 1
  • 1
  • 3
  • 1
    Your final two snippets are not valid Python code. Please don't post such code as it's hard to tell what's going wrong. – Fred Foo Nov 16 '12 at 10:58
  • http://ramblings.timgolden.me.uk/2010/07/02/python-3-print-or-print/ and http://docs.python.org/3.0/whatsnew/3.0.html#what-s-new-in-python-3-0 – Matt Seymour Nov 16 '12 at 11:01
  • @larsmans Not sure what you mean; they are perfectly valid (except for the fact that the print statement does not exist in Python 3). – poke Nov 16 '12 at 11:10
  • @poke Both are missing the colon after the `if`. This means the code was typed anew without much attention into SO and the structure of the original code might be something completely different. The indent of the `if` is also invalid - an indent **must** follow a statement that opens a block. So, no, the code samples aren't even close to valid. – millimoose Nov 16 '12 at 11:11
  • @millimoose I can see colons…? – poke Nov 16 '12 at 11:16
  • 2
    @poke Ah. That's an edit then. (SO hides edits done quickly after posting the question from history.) The original code was rather jumbled. – millimoose Nov 16 '12 at 11:18

3 Answers3

16

For Python 2, it makes no difference. There, print is a statement and 'hello' and ('hello') are its argument. The latter gets simplified to just 'hello' and as such it’s identical.

In Python 3, the print statement was removed in favor of a print function. Functions are invoked using braces, so they are actually needed. In that case, the print 'hello' is a syntax error, while print('hello') invokes the function with 'hello' as its first argument.

You can backport the print function to Python 2, by importing it explicitly. To do that add the following as the first import of your module:

from __future__ import print_function

Then you will get the same behaviour from Python 3 in Python 2, and again the parentheses are required.

Tommy
  • 99,986
  • 12
  • 185
  • 204
poke
  • 369,085
  • 72
  • 557
  • 602
9

You should read what's new in python 3.0:

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105).

Backwards Compatibility:

The changes proposed in this PEP will render most of today's print statements invalid. Only those which incidentally feature parentheses around all of their arguments will continue to be valid Python syntax in version 3.0, and of those, only the ones printing a single parenthesized value will continue to do the same thing. For example, in 2.x:

>>> print ("Hello", "world")  # without import
('Hello', 'world')

>>> from __future__ import print_function  

>>> print ("Hello", "world")       # after import 
Hello world
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • @hayden: It won't unless you import print_function from future – Abhijit Nov 16 '12 at 11:04
  • @Abhijit Not true; the parentheses for the print statement in Python 2 are ignored. Just in the same way they are ignored in `print ((((('test')))))`. – poke Nov 16 '12 at 11:07
  • @hayden: In python, using parenthesis around an expression is optional, unless you are invoking a callable (a function or a class). In python 2, `print` is a statement, so anything following the `print` statement is an expression and that makes the parenthesis optional there. – Martijn Pieters Nov 16 '12 at 11:08
  • @MartijnPieters `print('test')` works fine in Python 2.7.3... apologies this was the only thing I was referring to – Andy Hayden Nov 16 '12 at 11:10
  • @hayden but `print('test','test')` will return a tuple in 2.x. – Ashwini Chaudhary Nov 16 '12 at 11:11
  • 1
    @hayden: exactly, that's what I am saying. The parenthesis are part of the expression following the `print` statement. They are *not* interpreted as a call to a `python` function. Ashwini is quite right in pointing out that adding a comma would make that a *tuple*, not multiple arguments to the `print` function. – Martijn Pieters Nov 16 '12 at 11:11
1

I landed here on my search for regex to convert these syntaxes. Here is my solution for others:

Works well in old Python2 example scripts. Otherwise use 2to3.py for additional conversions.

Try it out on Regexr.com (doesn't work in NP++ for some reason):

find:     (?<=print)( ')(.*)(')
replace: ('$2')

for variables:

(?<=print)( )(.*)(\n)
('$2')\n

for label and variable:

(?<=print)( ')(.*)(',)(.*)(\n)
('$2',$4)\n

How to replace all print "string" in Python2 with print("string") for Python3?

alchemy
  • 954
  • 10
  • 17