'How do i actually wipe a printed line in python

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?



Solution 1:[1]

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)

Solution 2:[2]

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='')

Solution 3:[3]

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

Solution 4:[4]

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)

Solution 5:[5]

'\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 += " "

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 suplolx
Solution 2 Bi Ao
Solution 3 Electron X
Solution 4 Pear
Solution 5 Schnitte