以单行打印输出

use*_*312 6 python printing

我有以下代码:

>>> x = 0
>>> y = 3
>>> while x < y:
    ... print '{0} / {1}, '.format(x+1, y)
    ... x += 1
Run Code Online (Sandbox Code Playgroud)

输出:

1 / 3, 
2 / 3, 
3 / 3, 
Run Code Online (Sandbox Code Playgroud)

我希望我的输出像:

1 / 3, 2 / 3, 3 / 3 
Run Code Online (Sandbox Code Playgroud)

我搜索并发现在一行中执行此操作的方法是:

sys.stdout.write('{0} / {1}, '.format(x+1, y))
Run Code Online (Sandbox Code Playgroud)

还有另一种方法吗?sys.stdout.write()因为我不知道它与它有什么不同,所以我感觉不舒服print.

mb1*_*b14 6

您可以使用

打印"东西",

(使用尾随逗号,不插入换行符),所以试试这个

... print '{0} / {1}, '.format(x+1, y), #<= with a ,
Run Code Online (Sandbox Code Playgroud)