1

How can I overwrite a print output with another specific print output? For example:

print("Overwrite this line", end="\r")
print("I do not want to overwrite any line")
print("I want to overwrite the first line")

How can I overwrite the first print statement with the third print statement? I want to replace the first line, with the third line. The second line should remain as it is.

In this code example, the first line would be overwritten by the second one, but the I do not want that. I want the first line would be overwritten by the third one

Anm
  • 447
  • 4
  • 15
  • 1
    Does this answer your question? [How to change the location of the pointer in python?](https://stackoverflow.com/questions/27612545/how-to-change-the-location-of-the-pointer-in-python) – Tomerikoo Mar 27 '21 at 08:48
  • For Linux, the best option is probably [`curses`](https://docs.python.org/3.3/howto/curses.html#curses-programming-with-python) – Tomerikoo Mar 27 '21 at 09:12

3 Answers3

1

You can use ANSI escape sequences, as long as your terminal supports them (this is the case on Linux, I'm not sure about Windows)

In particular, the interesting ones for this problem are:

  • \033[<N>A - Move the cursor up N lines
  • \033[<N>B - Move the cursor down N lines

You can print the first two lines normally, then for the third one move up 2 lines, print it (this will print a newline and move the cursor to the second line), move down 1 line and continue with your code. I interted some delays in the code so that the effect is visible:

print("Overwrite this line")
time.sleep(1)
print("I do not want to overwrite any line")
time.sleep(1)
print("\033[2AI want to overwrite the first line\033[1B")
Dario Petrillo
  • 980
  • 7
  • 15
-1

You must use conditional statements. If statement for example:

choice = input('what do you want to display? 1 or 2')

if choice == '1' then:
    print('I want to overwrite the first line')
else:
    print('I do not want to overwrite any line')

Does this answer your question? If not, please elaborate.

Dario Petrillo
  • 980
  • 7
  • 15
-2

Use escape sequence \r for this purpose. And for wait use time.sleep

import time

print("Overwrite this line", end="")
time.sleep(10)
print("\rI want to overwrite the first line")
print("I do not want to overwrite any line")

For more precise result wait for key press. and check out this code:

import msvcrt as m
def wait():
    m.getch()

print("Overwrite this line", end="")
wait()
print("\rI want to overwrite the first line")
print("I do not want to overwrite any line")
Davinder Singh
  • 2,060
  • 2
  • 7
  • 22
  • third line didn't overwrite first line – Omkar76 Mar 27 '21 at 09:00
  • 2
    Well, you changed the meaning of the lines to make it look like it works... The second line says *"I __do not__ want to overwrite any line"* and the third one says *"I __want__ to overwrite the first line"*. Your code is doing the opposite – Tomerikoo Mar 27 '21 at 09:08
  • @AaravPrasad can i answer, using logic `first clear then reprint it` – Davinder Singh Mar 27 '21 at 09:16