使用函数的end参数覆盖其默认值,以便函数不会在末尾添加新行:print'\n'
print(text, end='')
Run Code Online (Sandbox Code Playgroud)
在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)