Harsh Parekh
Harsh Parekh

Reputation: 45

How to append a variable and print it in place of the same line before it was?

I want to print on a single line dynamically.

I have a list whose elements are being printed one below another if x condition matches. (Let x be any condition)

EG List :

[1 ,2, 4, 5, 6, 20, 11]

Irrespective of that condition being true or false, I want to create "x out of y elements" have been checked kind of dynamic statement.

EG : Consider the list contains 100 elements.

[20/100] have been checked.

[25/100] have been checked.

and so on ...

This number '20' should be dynamically updated on the very same line every time when a new element from the list is checked with my condition, irrespective of condition being true or false.

I searched a lot but didn't found what is this called or how to implement this. Please guide me.

Thanks in advance for any help !

Upvotes: 1

Views: 556

Answers (1)

K. Railis
K. Railis

Reputation: 184

You can do this by specifying the end argument in print() as '\r' or Carriage Return as it is known.

For instance:

for i in range(100):
    # Your check goes here
    print('[{}/100] have been checked.'.format(i), end='\r')

For the change to be noticeable you could use time.sleep() for a small interval like 0.5 secs. Something like this:

import time
for i in range(100):
    # Your check goes here
    print('[{}/100] have been checked.'.format(i), end='\r')
    time.sleep(0.5)

Upvotes: 2

Related Questions