print 只是一个瘦包装器,用于格式化输入(可修改,但默认情况下,在 args 和换行符之间有一个空格)并调用给定对象的 write 函数。默认情况下,此对象为sys.stdout,但您可以使用“chevron”形式传递文件。例如:
print >> open('file.txt', 'w'), 'Hello', 'World', 2+3
见:https://docs.python.org/2/reference/simple_stmts.html?highlight=print#the-print-statement
在 Python 3.x 中,print 成为一个函数,但由于 file 参数,它仍然可以传递 sys.stdout 以外的其他内容。
print('Hello', 'World', 2+3, file=open('file.txt', 'w'))
见https://docs.python.org/3/library/functions.html#print
在 Python 2.6+ 中,print 仍然是一个语句,但它可以作为函数使用
from __future__ import print_function
更新:Bakuriu 评论指出 print 函数和 print 语句之间(更一般地说是函数和语句之间)存在细微差别。
如果在评估参数时出错:
print "something", 1/0, "other" #prints only something because 1/0 raise an Exception
print("something", 1/0, "other") #doesn't print anything. The function is not called