0

i have a print function that prints a line every second, and it updates every time. for the most part ...end="\r" does the job but for some reason it doesnt really delete the previous line, it just overlaps it, meaning that the end of the previous line will sometimes just permanently be sticking out, making it unreadable and jarring to look at.

my function looks like this:

import time

def example():
    #some code setting up stuff
    while True:
        print("some calculation i'm too lazy to actually reproduce", end="\r")
        time.sleep(1)

i have been looking all over and i have so far been unable to find the solution or even people having the same question as mine, and im tired of seeing lines get printed with four s'es after it.

edit: in my actual code there is an actual calculation being done in the print statement with changing variables, this example is just an abridged version of my code.

couldnt i use flush=True for this or does that also turn out to do something completely different?

Undergod
  • 23
  • 2
  • Are you running the program in the OS terminal or some kind of IDE ? – teoML Mar 23 '22 at 14:32
  • Why would you expect carriage return to delete anything? – DarkKnight Mar 23 '22 at 14:33
  • Does this answer your question? [How can I clear a line in console after using \r and printing some text?](https://stackoverflow.com/questions/25142958/how-can-i-clear-a-line-in-console-after-using-r-and-printing-some-text) – matszwecja Mar 23 '22 at 14:39
  • Add. to duplicate - you might need `os.system("")` on Windows to avoid some garbage characters printed. – matszwecja Mar 23 '22 at 14:40

5 Answers5

0

You could print equal number of whitespaces to 'replace' the previous content by blanks:

import time

message = "some calculation i'm too lazy to actually reproduce"
print(message, end="\r")
time.sleep(1)
print(' ' * len(message), end='')

Bi Ao
  • 704
  • 5
  • 11
0

here's my approach, still overwriting, but more thoroughly

import time

def example():
    previous_line_len = 0
    while True:
        text_to_print = "some calculation i'm too lazy to actually reproduce"
        print(text_to_print + " "*(previous_line_len-len(text_to_print)), end="\r")
        previous_line_len = len(text_to_print)
        time.sleep(1)

this simply adds whitespace to cover previous line

Electron X
  • 330
  • 1
  • 10
0

You could store the length of output to a variable and use it to pad spaces.

# Example to demonstrate
import time

def example():
    #some code setting up stuff
    longText = "qwertyuiopasdfghjklzxcvbnm"
    
    #initially set the length of output to 0
    lenVar = 0

    while True:

        toPrint = "some calculation i'm too lazy to actually reproduce! "+longText

        #pad spaces if necessary
        toPrint+=" " * (0 if lenVar<=len(toPrint) else lenVar-len(toPrint))
        print(toPrint, end="\r")
        
        # store the length of output in a variable
        lenVar = len(toPrint)

        # decrease the longText!
        longText = longText[:-1]

        time.sleep(1)
Pear
  • 351
  • 2
  • 12
0

You could also add spaces to the line you're printing and it will overlap the previous line without adding letters like this:

import time

def example():
    #some code setting up stuff
    while True:
        print("some calculation i'm too lazy to actually reproduce  ", end="\r") # added spaces here
        time.sleep(1)
Classi
  • 175
  • 2
  • 7
-1

'\r' is carriage return; it moves the pointer back to the beginning of the line, but doesn't include a line break. So when a print statement ends with '\r', the next print statement will be printed on the same line as the one before, but if the first line was longer, then the excess letters will remain at the end instead of being overwritten. If you really want to get rid of these excess letters, you can just pad the second line with spaces until it is as long as the previous one:

text_for_first_line = "some longer string"
text_for_second_line = "some string"
for _ in range(len(text_for_first_line) - len(text_for_second_line)):
    text_for_second_line += " "
Schnitte
  • 1,193
  • 4
  • 16