为什么只有字符串应该打印时才会打印打印功能中的整个参数以及paranthesis
这是Python 2.7.9
import os
alist = [ 'A' ,'B']
print('Hello there')
print('The first item is ',alist[0])
print('Good Evening')
root@justin:/python# python hello.py
Hello there
('The first item is ', 'A')
Good Evening
Run Code Online (Sandbox Code Playgroud)
在python 2中print它不是一个函数,它是一个声明.当你写作
print('The first item is ',alist[0])
Run Code Online (Sandbox Code Playgroud)
它其实就是"打印我2个元素的元组:'The first item is '和alist[0]"
它相当于
a = ('The first item is ',alist[0])
print a
Run Code Online (Sandbox Code Playgroud)
如果你只想打印字符串,你应该删除括号:
print 'The first item is ',alist[0]
Run Code Online (Sandbox Code Playgroud)
编辑:正如评论中的家伙所说,你也可以添加
from __future__ import print_statement
Run Code Online (Sandbox Code Playgroud)
这将产生print类似于python 3的函数,您的示例将按预期工作而不进行任何更改.
但我认为理解两种情况下的情况都很有用.
| 归档时间: |
|
| 查看次数: |
543 次 |
| 最近记录: |