使用逗号
print("Hello", end=" ")
print("World")
使用`end`参数
print("Hello", end=" ")
print("World", end=" ")
使用`print`函数的返回值
print("Hello", end=" ")
print("World", end=" ")
使用`join`方法
print(" ".join(["Hello", "World"]))
使用`format`方法
print("{} {}".format("Hello", "World"))
使用`f-string` (Python 3.6及以上版本):
print(f"Hello World")
使用`sys.stdout.write`
import sys
sys.stdout.write("Hello World\n")
使用`print`函数作为函数调用
def print_hello_world():
print("Hello", end=" ")
print("World", end=" ")
print_hello_world()
使用`with open`和`print`
with open("output.txt", "w") as f:
f.write("Hello World\n")
使用`print`函数在循环中
for i in range(5):
print("Hello World", end=" ")
以上方法都可以实现连续输出,具体使用哪种方法取决于你的需求和Python版本。需要注意的是,在Python 2.x版本中,`print`是一个语句而不是一个函数,因此语法稍有不同。