0

I'm trying to print in Jupyter Lab in the same line, but clearing the previous output. I'm aware of the '\r' escape character and that I can do print('\r'+ my_string,end='') and it works. The thing that I want to know is how can I completely erase the previous output, because when my_string has less characters, the characters from previous line output remains at the end of the line.

For example:

my_string = '\r{}%'
for i in range(10000,-1,-1):
    print(s.format(i/100),end='')

The final output for this would be 0.0%%%, instead of 0.0%

I tried this SO post, but it didn't work for me :(

EDIT: What I'm trying to accomplish is something like this:

my_string = '\rLap {} || {}%'
for lap in range(3):
    for i in range(10000,-1,-1):
        print(my_string.format(lap,i/100),end='')
    print()

Actual output:

Lap 0 || 0.0%%%
Lap 1 || 0.0%%%
Lap 2 || 0.0%%%

Desired output:

Lap 0 || 0.0%
Lap 1 || 0.0%
Lap 2 || 0.0%
xerac
  • 147
  • 8

1 Answers1

0

You could use IPython to clear/overwrite the output:

pip install IPython
from IPython.display import clear_output
clear_output(wait=True)

Also try using the os module:

import os
print('Hello...')
os.system('clear') #os.system('cls') for Windows
print ("cleared!")

or try putting \r in the end argument.

print('', end='\r')
krmogi
  • 2,588
  • 1
  • 10
  • 26
  • Thanks @krmogi. This would actually erase the complete output of the cell right? Isn't a way to just clear/erase the current line or how to avoid having those characters left from previous printing? – xerac Oct 24 '21 at 16:22
  • I updated my answer, try those as well. – krmogi Oct 24 '21 at 16:27
  • Thanks for the update. I tried what you proposed, but I couldn't achieve it :(. I also edited the question and updated the desired output, hoping to be more clear. – xerac Oct 25 '21 at 17:36