Unprint a line on the console in Python?

后端 未结 3 1753
难免孤独
难免孤独 2020-12-17 03:16

Is it possible to manipulate lines of text that have already been printed to the console?

For example,

import time
for k in range(1,100):
     print(         


        
3条回答
  •  臣服心动
    2020-12-17 03:24

    The simplest method (at least for Python 2.7) is to use the syntax:

    print 'message', '\r',
    print 'this new message now covers the previous'
    

    Notice the extra ',' at the end of the first print. This makes print stay on the same line. Meanwhile, the '\r' puts the print at the beginning of that line. So the second print statement overwrites the first.

提交回复
热议问题