In python, how do I make a string overlap a current string in the shell?

In python, how do I make a string overlap a current string in the shell?

Problem Description:

Each second, it prints a new line.
Is there a way to have it print ontop of the previous line?

while True:
    sec += 1
    if sec / 60 == sec_int:
        sec = 0
        mins += 1
        if mins / 60 == min_int:
            mins = 0
            hours += 1
            if hours / 24 == hour_int:
                hours = 0
                days += 1
    print(f"{days}d : {hours}h : {mins}m : {sec}s")
    time.sleep(1)

Solution – 1

Replace your print statement with:

print(f"r{days}d : {hours}h : {mins}m : {sec}s", end="", flush=True)

"r" is a "control character" which moves the cursor to the beginning of the line ("carriage Return"). flush=True is needed to make the display update right away–normally Python can buffer until a newline is written, which of course you’ll never write (because there’s no way to go back up to a previous line).

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject