Member Avatar

Ok, I'm confused, what is the difference between

print "stuff"



They both have the same output!

Recommended Answers

All 2 Replies

print is statement in Python 2, and brackets around single value do nothing, so basically both are Ok in Python2. In Python3 print is function, which is usefull. The same style of printing can be activated in Python2 by

 from  __future__ import print_function 

So the first version fails in Python3. I generally use future import and Python3 print style, but if you do not want to confuse newbies in both versions, you can use single parameter bracketed prints, and can consentrate in subject matter withput concerm of Python versions if subject matter itself is compatible.

In python 2, there is no difference between

print "foo"
print ("foo")

because print is a statement and the expression ("foo") has the same value as the expression "foo". On the other hand the statements

print "foo", "bar"
print ("foo", "bar")

are not equivalent because ("foo", "bar") is a single expression which value is a tuple of length two, so the first statement prints two strings while the second statement prints a tuple containing two strings.
In python 3, print() is a function, so print "foo" and print "foo", "bar" are syntax errors. The parenthesis in print ("foo", "bar") are ordinary function call parenthesis, they don't mean build a tuple and two strings are printed. To print the same tuple in python 3, one must write print(("foo", "bar")).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.