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="")
添加回答
舉報