How to overwrite the previous print to stdout in python?

后端 未结 16 1570
别那么骄傲
别那么骄傲 2020-11-22 09:46

If I had the following code:

for x in range(10):
     print x

I would get the output of

1
2
etc..

What I

16条回答
  •  孤独总比滥情好
    2020-11-22 10:32

    I couldn't get any of the solutions on this page to work for IPython, but a slight variation on @Mike-Desimone's solution did the job: instead of terminating the line with the carriage return, start the line with the carriage return:

    for x in range(10):
        print '\r{0}'.format(x),
    

    Additionally, this approach doesn't require the second print statement.

提交回复
热议问题