您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python:使用print命令避免换行

Python:使用print命令避免换行

在 ,可以使用函数end参数print()来防止换行符被打印:

print("Nope, that is not a two. That is a", end="")

在 ,可以使用尾部逗号:

print "this should be",
print "on the same line"

不过,您不需要此即可简单地打印变量:

print "Nope, that is not a two. That is a", x

请注意,尾部逗号仍会导致在行尾打印一个空格,即等同于end=" "在Python 3中使用。要抑制空格字符,也可以使用

from __future__ import print_function

可以访问Python 3打印功能或使用sys.stdout.write()

python 2022/1/1 18:32:47 有207人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶