我想在同一行上将循环输出打印到屏幕上,如何以最简单的方式实现Python 3.x?我知道这个问题已经在Python 2.7中使用在行末加逗号即"print I,"的方式进行了解决,但我找不到一个适用于Python 3.x的方法。
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
,
将多个字符串连接在一起,并使用end
参数指定末尾字符为空格而不是新行符。这样,后续的输出就会在同一行上。例如,以下代码将打印数字 0 到 9,每隔一秒钟输出一个数字,并保持所有数字都在同一行上:import time for i in range(10): print(i, end=' ') time.sleep(1)
输出结果如下:0 1 2 3 4 5 6 7 8 9
- Nanhe Kumar