Python中打印的控制行结束

2024-03-28 16:41:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我在别处读到,我可以通过在语句末尾添加“,”来防止print转到下一行。然而,有没有办法有条件地控制这种情况?有时结束行,有时不基于变量?你知道吗


Tags: 情况语句条件print末尾办法
3条回答

这不是最优雅的方式,但也是一种选择。你知道吗

import sys
sys.stdout.write("must be a string")
sys.stdout.flush()

一种解决方案,无需将来导入:

print "my line of text",
if print_newline:
    print

相当于:

from __future__ import print_function
print("my line of text", end="\n" if print_newline else "")
from __future__ import print_function

print("foo", end="" if no_line_br else None)

你该走了。None表示默认值,即"\n"。你知道吗

相关问题 更多 >