打印执行下一条指令后的Python尾随逗号 [英] Python trailing comma after print executes next instruction

查看:26
本文介绍了打印执行下一条指令后的Python尾随逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在打印语句的末尾添加了尾随逗号,则首先执行下一条语句.为什么是这样?例如,这会在打印 "Hi " 之前执行 10000 ** 10000:

If a trailing comma is added to the end of a print statement, the next statement is executed first. Why is this? For example, this executes 10000 ** 10000 before it prints "Hi ":

print "Hi",
print 10000 ** 10000

这需要一段时间才能打印Hi Hello":

And this takes a while before printing "Hi Hello":

def sayHello():
    for i in [0] * 100000000: pass
    print "Hello"
print "Hi",
sayHello()

推荐答案

  1. 在 Python 2.x 中,print 语句中的尾随 , 可防止发出新行.

  1. In Python 2.x, a trailing , in a print statement prevents a new line to be emitted.

  • 在 Python 3.x 中,使用 print("Hi", end="") 可以达到相同的效果.
  • In Python 3.x, use print("Hi", end="") to achieve the same effect.

标准输出是行缓冲的.所以 "Hi" 在发出新行之前不会被打印出来.

The standard output is line-buffered. So the "Hi" won't be printed before a new line is emitted.

这篇关于打印执行下一条指令后的Python尾随逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆