无需换行即可打印

exe*_*ook 1 python

如何在没有添加换行的情况下在Python中打印某些内容?

C或Node.js中的某些内容printf("abc")(最后没有\n)process.stdout.write("abc")

pok*_*oke 5

Python 3:

使用函数end参数覆盖其默认值,以便函数不会在末尾添加新行:print'\n'

print(text, end='')
Run Code Online (Sandbox Code Playgroud)

Python 2:

print语句中添加一个尾随逗号:

print text,
Run Code Online (Sandbox Code Playgroud)

如果您需要适用于Python 2和Python 3的解决方案,您应该考虑print在Python 2中导入该函数,因此您可以在两者上使用上述Python 3语法.您可以使用模块顶部的以下行执行此操作:

from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)