Why do `print("Hello, World!")` and `print("Hello", "World!")` produce different outputs?
I just installed Python 2.7.2 on Windows XP with the idea of learning how to program. Several of the tutorial books I'm using give examples of print commands which, when I try them, I get diff开发者_如何学运维erent answers.
I expected both of these to return the same thing -
>>> print("Hello, World!")
Hello, World!
>>> print("Hello", "World")
('Hello', 'World')
>>>
I've tried searching around for answers, but I'm not even sure how to explain where I'm going wrong.
Since print
is a statement in Python 2.x, you're getting expected behavior. (a,b)
is a tuple, so print (a,b)
will print it as a tuple.
On the other hand, print
is a function in Python 3.x, so
print("hello world")
and
print("hello", "world")
will yield the same answer.
This is one breaking change when going from Python 2.x to 3.x. Understanding the difference is important. Type help()
in your interpreter and then print
. You will get different descriptions based on your Python version.
I'd suggest checking out this page, which describes in a quick and nice way what worked before and what works now.
In the first case, you are calling the print
method passing "Hello World!"
as an argument. In Python 2.7 you don't need the braces, so you could also write it as
print "Hello World!"
In the second case you create a tuple ("Hello", "World")
and pass that one to print
. Therefore the tuple is printed. You are not passing two parameters to print
. You are passing just one tuple.
It is not what you think it is. It's one argument and it is a tuple.
x=("Hello", "World")
>>> type(x)
<type 'tuple'>
And the reason that it works like this is because print is not a function but a statement.
Note: In python 3 print is a function.
in Python 2.x, print is a statement and not function. Statements have no paranethesis
print 'Hello World'
That's a bit tricky and will change in Python 3. You can activate this behavior in python 2 by adding the following import at the beginning of your sctipt:
from __future__ import print_fuction
Then, print will become a function and you'll call it like this
print('Hello World')
The difference you see in your example is that ('Hello', 'World') is a tuple while ('Hello World') is a string.
As mentionned by the Python doc :
A tuple consists of a number of values separated by commas
There is no coma in ('Hello world') so it is not a tuple. But ('Hello world',) has a coma before the end of the paranthesis and is a tuple.
The parenthesis in ('Hello world') are valid but are not really useful here.
You sould be careful with this when getting started. Tuples are identified by comas not parenthesis. Except for () which is the empty tuple. As we say in french, "this exception is the confirmation of the rule"
The difference in those two lines:
print("Hello, World!")
-> One string is printed, is says 'Hello, World!'
print("Hello", "World")
-> Two strings are given and both are printed. First 'Hello' and then 'World'
精彩评论