在同一行上打印新的输出
我想在同一行上将循环输出打印到屏幕上。
我如何以最简单的方式为Python 3.x
我知道这个问题已经被要求在Python 2.7的末尾使用逗号,即打印我,但是我找不到Python 3.x的解决scheme。
i = 0 while i <10: i += 1 ## print (i) # python 2.7 would be print i, print (i) # python 2.7 would be 'print i,' 屏幕输出。
 1 2 3 4 5 6 7 8 9 10 
我想要打印的是:
 12345678910 
新读者访问此链接以及http://docs.python.org/release/3.0.1/whatsnew/3.0.html
 从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 。 
*注意:这个代码只适用于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. 
你可以做一些事情,如:
 >>> print(''.join(map(str,range(1,11)))) 12345678910 
让我们举一个例子,你想在同一行打印0到n的数字。 你可以在下面的代码的帮助下做到这一点。
 n=int(raw_input()) i=0 while(i<n): print i, i = i+1 
input时,n = 5
 Output : 0 1 2 3 4 
像所build议的类似,你可以这样做:
 print(i,end=',') 
输出:0,1,2,3,
 >>> for i in range(1, 11): ... print(i, end=' ') ... if i==len(range(1, 11)): print() ... 1 2 3 4 5 6 7 8 9 10 >>> 
这是如何做到这一点,以便打印不会运行在下一行的提示后面。
 print("single",end=" ") print("line") 
这会给出输出
 single line 
为问的问题使用
 i = 0 while i <10: i += 1 print (i,end="")