3

I was reading about python functions and saw this code:

def happyBirthday(person):
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear " + person + ".")
    print("Happy Birthday to you!")

happyBirthday('Emily')
happyBirthday('Andre')

I couldn't understand why these brackets were being used for the print commands and so I removed them.

def happyBirthday(person):
    print "Happy Birthday to you!"
    print "Happy Birthday to you!"
    print "Happy Birthday, dear " + person + "."
    print "Happy Birthday to you!")

happyBirthday('Emily')
happyBirthday('Andre')

Even after removing those brackets I am getting the exact same results, so I am not sure which one is correct or whether I should use those brackets at all. Is it really necessary to use those brackets?

One more thing. when I use the brackets then the +person+ gives the result as Happy Birthday, dear Andre. but when I use ,person, then it gives the result as <'Happy Birthday,dear ',' 'Andre','.'>

I am unable to understand these differences in the results. Could you shed some light on this?

0

4 Answers 4

13

Is it really necessary to use those brackets?

In Python 2.x, print is a statement, and the brackets are optional.

In Python 3.x, print() is a function, and the brackets are mandatory.

It is considered good practice to use brackets even in Python 2.x, to ease eventual transition to Python 3.x.

I am unable to understand these differences in the results. Could you shed some light on this?

Here is what happens when you print several comma-separated things in Python 2.x:

In [1]: print(1,2,3)
(1, 2, 3)

The above is interpreted as the print statement followed by a single argument, which is a tuple. The tuple is rendered with parentheses and commas.

In [2]: print 1,2,3
1 2 3

The above is interpreted as the print statement followed by three arguments. Each argument is printed out separately, with spaces between them.

Neither version is great as far as compatibility with Python 3 is concerned: the first version is rendered differently, and the second is simply not valid Python 3 code.

With this in mind, I recommend that you stick with:

print("Happy Birthday, dear " + person + ".")

This produces exactly the same results in both Python 2.x and Python 3.x.

Sign up to request clarification or add additional context in comments.

7 Comments

It's worth noting why this is. In Python 2.x, print is a statement (like return or if), while in 3.x, it is a function (like sum() or all()).
and so like every function. we have to use brackets. In short, it is bettter to use brackets. right?
Here's the documentation of this change: docs.python.org/release/3.0.1/whatsnew/…
It is optional to consider this good practice. If you later need your code Python 3 compatible you can use a converter: docs.python.org/library/2to3.html
To ease the transition to Python3, better to use from __future__ import print_function
|
3

In Python2, print is a statement. In Python3 it is a function

For some simple cases it works the same with the parentheses, but in general it is not as easy as just slapping parens around the print statement to port it to Python3

eg.

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hi",
hi
>>> print("hi",)
('hi',)

vs

Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hi",
  File "<stdin>", line 1
    print "hi",
             ^
SyntaxError: invalid syntax
>>> print("hi",)
hi

2 Comments

I am not using python 3 . Both of these results are from python 2.7
@faraz, obviously otherwise the parentheses would be mandatory
1

As already mentioned, print is a statement in Python 2 Then, when you do print('xxx'), the whole

('xxx')

is interpreted first. Single parens are ignored, so this is simply a string which is printed.

When you do 'string' + name + 'string', ther strings are first concatenated (added), resulting in a string. It is then evaluated in parens, returning itself, and then printed.

On the opposite, when you do print('x', 'y', 'z'), the whole

('x', 'y', 'z')

is a tuple, which is printed as <'Happy Birthday,dear ',' 'Andre','.'>

3 Comments

hmm, Okay, what i did now is I removed the +person+ and so now that line is only print ("Happy Birthday, dear "".") but I get the result as Happy Birthday, dear. Why isn't this acting as a tuple? I mean shouldn't this be the same as ('x','y')
Ah, got it. I didnt put comma in there and therefore it wasn't acting as a tuple. Now, when I put in a comma it is acting as a tuple. The whole line was: print ("Happy Birthday, dear "".") which doesnt include a comma. But when i wrote it as ("Happy Birthday, dear","."), it gives the result exactly as you mentioned. I get it now. Thanks
Yes, 1 more things - consequent strings are concatenated automatically.
0

try str(person) in print staement

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.