0

I have looked at all of these and more, this is not a duplicate! (well it is but I need an answer).

How to overwrite multiline print in Python?

How can I remove last printed line in python?

Output to the same line overwriting previous output?

Overwrite the previous print value in python?

But not even one of these had an answer that actually overwrites a printed character in Python 3.8.

So let me ask how can you overwrite the last printed character in Python?

For instance in this code:

import time
print("Line one",end="\r")
time.sleep(2)
print ("Line two")

In this code I want "Line one" to be replaced with "Line two".

or maybe,

import time 
print("1")
time.sleep(1)
print("2")
time.sleep(1)
print("3")

So that each number replaced the last one.

KOTZ
  • 125
  • 9
  • why the need for `time` module? – FishingCode Mar 20 '20 at 03:30
  • 1
    Just adding a time delay between creation and the replacement of the line. – KOTZ Mar 20 '20 at 03:31
  • Where is your output going? In the IDLE shell, for example, absolutely *nothing* you can do is going to result in overwriting previous output - it's just appending characters to a Text widget. – jasonharper Mar 20 '20 at 03:35
  • How about in VSC? Or in the cmd – KOTZ Mar 20 '20 at 03:37
  • In cmd you could always try printing an ascii backspace "\b". A little hacky, but it will work – R. Davidson Mar 20 '20 at 03:44
  • What's the behavior with your code that isn't working - in PyCharm the only visible output is `Line two` - is that not what you want. FYI as @R.Davidson said, you can use `\b` (backspace) to move back character by character. You can use multiple ones like `end='\b\b\b\b\b'` – DaveStSomeWhere Mar 20 '20 at 04:03
  • 1
    Your code works for me. Note that the second line needs to be at least as long as the first line, otherwise you'll still see the end of the first line. – Barmar Mar 20 '20 at 04:07
  • 1
    Make sure you're running the code in a terminal emulator, not a debugger like IDLE. – Barmar Mar 20 '20 at 04:09
  • So I ran the code in the cmd and it print's out "Line two", But it doesn't print "Line one" then wait 2 seconds and print "Line two". Is there a way to achieve that in the cmd? – KOTZ Mar 20 '20 at 04:40
  • Never mind I figured it out, and by the way I was trying to find a way that worked in IDLE, But if it works in the cmd that's fine anyway, Thanks guys! – KOTZ Mar 20 '20 at 04:44

1 Answers1

1

Above all,I approve of your method that printing with an ending '\r',this method is right but can't show the result that you want in your Code editor such as 'sublime text3'.

In fact,this method can show the right result in Cmd. Here are three examples hoped to help you.

import time 
print("EXAMPLE I")
print("Line one",end="\r")
time.sleep(1)
print("Line two")

if it is in Code editor,it will show:

EXAMPLE I
Line one
Line two

maybe you will think that this result is same as the '\n' one.But this is not the case.if we run it in Cmd it will show these results:

EXAMPLE I
Line two

Is it amazing?In fact the Line one has replaced by the Line two which achieves your aim.

By the way, may I show you the other way which mentioned in the comments above.

print("EXAMPLE II")
print("Line one",end="\n")
time.sleep(1)
print("Line two")

in Code editor

EXAMPLE II
Line one
Line two

in Cmd

EXAMPLE II
Line one
Line two

CODE

print("EXAMPLE III")
print("Line one",end="\b")
time.sleep(1)
print("Line two")

in Code editor

Line one<0x08>Line two

in Cmd

Line onLine two

pay more attention: the '\b' ending just has the capablility of backing one word, not the whole line. You may see the difference between CMD and Code editor.

Dharman
  • 21,838
  • 18
  • 57
  • 107
moqin
  • 52
  • 5