如何在没有回车的情况下在Python 3.3.3中打印?

RGS*_*RGS 5 python printing string string-formatting python-3.3

首先,我不是在寻找如何与同一行打印print(some_stuff, end="").

在Python 2.7中,您可以输入:

while True:
for i in ["/","-","|","\\","|"]:
    print "%s\r" % i,
Run Code Online (Sandbox Code Playgroud)

并且它会在相同的行中打印出SAME中的那5个字符,使它看起来像是一个条形旋转(实际上你可以尝试一下).问题是,我不能在Python 3.3中做同样的事情,我已经尝试了几件事.我的具体应用是倒数计时器...代码是这样的:

import time
t = 120
while t > 0:
    t -= 1
    print("Time left till next update: %d seconds" % t)
    time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

输出应该是字符串,只有在适当的位置改变的秒数...希望有人可以帮我这个.

Mar*_*ers 10

您使用end='\r'关键字参数:

print("Time left till next update: %d seconds" % t, end='\r')
Run Code Online (Sandbox Code Playgroud)

默认end值为'\n'但您可以指定自己的默认值.

  • @RSerrao:我在一个终端测试了这个,它运行得很好.也许IDLE不再支持`\ r``了? (2认同)