print语句
在python2.x中,print语句最简单的使用形式是
print hello world!
这相当于执行了
sys.stdout.write(‘’。join(map(str,[hello world!]))+'\n')
如果以逗号为分隔符,传递额外的参数,这些参数会被传递到str()函数,最终打印的时候,每个参数之间会空一行。
从2.0版本开始,python引入了print>>的语法。作用是重定向print语句最终输出的字符串的文件
例如:
print>>output
相当于
output.write(str(hello)+’\n’)
print函数
如果用python来实现print的函数,他的函数定义应该是这样的
import sys def print(*objects, sep=None, end=None, file=None,
flush=False):
“”“A Python translation of the C code for builtins.print().
“””
if sep is None:
sep = ’ ’
if end is None:
end = ‘\n’
if file is None:
file = sys.stdout
file.write(sep.join(map(str, objects)) + end)
if flush:
file.flush() 函数定义
从上面的代码我们可以发现,python3.x的print 函数实现了print语句的所有特性。
版权声明:本文为JonyQAQ原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。