3 回答

TA贡献1817条经验 获得超6个赞
来自help(print):
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
您可以使用end关键字:
>>> for i in range(1, 11):
... print(i, end='')
...
12345678910>>>
请注意,您必须自己完成print()最终换行。顺便说一句,你不会在Python 2中使用尾随逗号得到“12345678910”,你会得到它1 2 3 4 5 6 7 8 9 10。

TA贡献1790条经验 获得超9个赞
* for python 2.x *
使用尾随逗号来避免换行。
print "Hey Guys!",
print "This is how we print on the same line."
上面代码片段的输出是,
Hey Guys! This is how we print on the same line.
* for python 3.x *
for i in range(10):
print(i, end="<separator>") # <separator> = \n, <space> etc.
上面代码片段的输出是(当<separator> = " "),
0 1 2 3 4 5 6 7 8 9

TA贡献1802条经验 获得超10个赞
print("single",end=" ")
print("line")
这会产生输出
single line
对于要求使用的问题
i = 0
while i <10:
i += 1
print (i,end="")
添加回答
举报