请解释这个输出 [英] Please explain this output

查看:52
本文介绍了请解释这个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

print ("Hello" , "World")

输出是:('Hello', 'World').为什么不仅仅是Hello World?(我对 python 很陌生.3 周前开始学习.)

The output is : ('Hello' , 'World'). Why is it not just Hello World? (I am very new to python. Started learning 3 weeks ago.)

推荐答案

在 python 2.7 中我们有

In python 2.7 we have

print('hello', 'world')
print ('hello', 'world')
print ('hello , world')

输出:

('hello', 'world')
('hello', 'world')
hello , world

打印是一个声明.就像那样传递参数.因此,如果我们在括号内有一个对象,它就可以正常工作.但是如果括号内有多个对象,它们会作为元组对象传递.元组会像元组一样被打印出来.

Print is a statement. Which passes the parameter just like that. So if we have one object inside the parenthesis it works fine. But if there are multiple objects inside the parenthesis they get passed as a tuple object. And A tuple will get printed like a tuple.

基本上

print('a', 'b')

a = ('a', 'b')
print(a)
# or
print a

# output : ('a', 'b')

这正是您所期望的.

另一方面,在python 3 Print 是一个函数.所以你必须调用它.基本上现在 'hello', 'world' 不是元组.而是将多个参数分别传递给打印函数.

On the other hand in python 3 Print is a function. So you have to call it. Basically now 'hello', 'world' is not a tuple. Rather multiple arguments being passed separately to print function.

print('hello', 'world')

输出:

hello world

为了在 3.5 中达到同样的效果,我们必须这样做

To achieve the same effect in 3.5 we will have to do this

print(('hello', 'world'))

输出:

('hello', 'world')

您可以在此处阅读更多信息:http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

You can read up more here: http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

这篇关于请解释这个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆