Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between print and print() in python 2.7

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)
like image 656
Tail of Godzilla Avatar asked Nov 30 '15 10:11

Tail of Godzilla


People also ask

What is the difference between print () and return () in Python?

Printing and returning are fundamentally different concepts in Python. Printing means displaying a value in the console. To print a value in Python, you call the print () function. Returning is used to return a value from a function and exit the function. To return a value from a function, use the return keyword.

What is the use of print in Python?

Python is popular because of advantages, like simple syntax and readability. Python is used over other languages because of its simplicity. The print function is used to display any form of message like string or calculated value. The input function is a built-in function in Python used to read a line of a text given by the user.

Why does print() not work in Python 2?

While print () works in Python 2, it is not doing what you may think. It is printing a tuple if there is more than one element: For the limited case of a one element parenthesis expression, the parenthesis are removed: But this is just the action of the Python expression parser, not the action of print:

What are the different styles of print statements in Python programming?

Different Styles Of Print Statements In Python Programming. 1 1)Numbered Indexes PROGRAM OUTPUT The two numbers are 10 and 20. 2 2)Named Indexes PROGRAM OUTPUT. 3 3)EMPTY PLACEHOLDER PROGRAM OUTPUT.


1 Answers

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.

like image 77
RemcoGerlich Avatar answered Oct 13 '22 07:10

RemcoGerlich