Print in one line dynamically

后端 未结 20 3193
梦谈多话
梦谈多话 2020-11-21 23:32

I would like to make several statements that give standard output without seeing newlines in between statements.

Specifically, suppose I have:

for it         


        
20条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 23:43

    So many complicated answers. If you have python 3, simply put \r at the start of the print, and add end='', flush=True to it:

    import time
    
    for i in range(10):
        print(f'\r{i} foo bar', end='', flush=True)
        time.sleep(0.5)
    

    This will write 0 foo bar, then 1 foo bar etc, in-place.

提交回复
热议问题