English 中文(简体)
希望简单的Python好奇心
原标题:Hopefully simple Python curiosity
  • 时间:2011-02-08 20:30:55
  •  标签:
  • python-2.6

我是python的新手,正在为我的工作功能学习python。我遵循一个非常基本的初学者教程,其中大部分看起来非常熟悉,与我使用的其他语言相似。但是当我做非常简单的事情

print( hello world )

我得到了预期的回复

hello world

但当我做另一项简单的任务时:

x = 5
print( x is , x)

我得到:

( x is , 5)

print命令保留了括号和单引号,就我个人而言,我不知道为什么。

最佳回答

由于您使用的是Python2.6,print是一个语句,而不是一个函数。因此,参数不应该在括号中(请注意,这在3.0版本中发生了更改)。

此代码将执行您想要执行的操作:

print  x is , x

您的原始代码实际上创建了一个元组并将其打印出来。

问题回答

print语句的使用是将类型回显为字符串(x,1)是一个元组。

Python计算(x为,5),然后转换结果(atuple)转换为字符串,即(x是,5)。去掉括号即可获得所需内容:

print  x is , 5

请参阅Python打印文档了解更多信息。





相关问题
How to install python2.6-devel package under CentOs 5

I need to install mysql-python under python2.6. mysql-python package needs python2.6-devel package that depends on the libpython2.6.so.1.0(64bit) I found on the net some python2.6-devel packages, but ...

Python try...except comma vs as in except

What is the difference between , and as in except statements, eg: try: pass except Exception, exception: pass and: try: pass except Exception as exception: pass Is the second ...

importing files in python

I have that file structure- BlogDataObjectsUser.py Blogindex.py I want to import the function(say_hello) at User.py from index.py. I am trying this code - from Blog.DataObjects.User import ...

Cannot use Python 2.6 C interface anymore, but 2.5 works

I just noticed that I cannot use the Python 2.6 dll anymore. Python 2.5 works just fine. import ctypes py1 = ctypes.cdll.python25 py2 = ctypes.cdll.python26 # ctypes.cdll.LoadLibrary("libpython2.6....

There is no spawnl function in python 2.6?

I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw. >>> os.spawnl(os.P_NOWAIT,"setup.exe") Traceback (most recent call last): File "<stdin&...

热门标签