0

i am trying to create a line of code that prints the first line like it normally would do then after printing it, it will then erase that output and write something else

print("This text is about to vanish - from first line",end='')
import time
time.sleep(3)
print("\rSame line output by Thirumalai")

i have this code from another user, how would i go about having it so that it shows the first line aswell, because when i run the py in terminal it doesnt do that, im using ubuntu

xellerate
  • 1
  • 3

1 Answers1

0

To make the first line appear, you need to turn off buffering. You can specify

print("This text is about to vanish - from first line",end='', flush=True)
#                                                              ~~~~~~~~~~

for the print (Python 3.3+ needed, see here for the details and ways how to do that in older versions).

You also need to print some spaces at the end of the second print to overwrite the remaining text.

print("\rSame line output by Thirumalai", ' ' * 15)
choroba
  • 231,213
  • 25
  • 204
  • 289