python的打印语句_Python Language

在Python 2中, print是一个声明:

Python 2.x 2.7

print "Hello World"

print # print a newline

print "No newline", # add trailing comma to remove newline

print >>sys.stderr, "Error" # print to stderr

print("hello") # print "hello", since ("hello") == "hello"

print() # print an empty tuple "()"

print 1, 2, 3 # print space-separated arguments: "1 2 3"

print(1, 2, 3) # print tuple "(1, 2, 3)"

在Python 3中, print()是一个函数,带有常用的关键字参数:

Python 3.x 3.0

print "Hello World" # SyntaxError

print("Hello World")

print() # print a newline (must use parentheses)

print("No newline", end="") # end specifies what to append (defaults to newline)

print("Error", file=sys.stderr) # file specifies the output buffer

print("Comma", "separated", "output", sep=",") # sep specifies the separator

print("A", "B", "C", sep="") # null string for sep: prints as ABC

print("Flush this", flush=True) # flush the output buffer, added in Python 3.3

print(1, 2, 3) # print space-separated arguments: "1 2 3"

print((1, 2, 3)) # print tuple "(1, 2, 3)"

打印功能具有以下参数:print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

sep是将传递给打印的对象分开的内容。例如:print('foo', 'bar', sep='~') # out: foo~bar

print('foo', 'bar', sep='.') # out: foo.bar

end是print语句的结尾。例如:print('foo', 'bar', end='!') # out: foo bar!

在非换行结束打印语句后再次打印将打印到同一行:print('foo', end='~')

print('bar')

# out: foo~bar

注意:为了将来的兼容性, print 功能也可以在Python 2.6以后使用;但是除非禁用解析print 语句,否则不能使用它from __future__ import print_function

此函数与Python 3的格式完全相同,只是它缺少flush参数。

有关基本原理,请参阅PEP 3105 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值