Home > Net >  How to replace console output when the string gets shorter?
How to replace console output when the string gets shorter?

Time:04-30

I know that i can replace console output instead of adding a new line with "\r" But how to do that with an output that can get smaller as it started? I googled and only found how to do it with a progressbar.

CodePudding user response:

the solution from Caius Jard works.. Its so simple yet so good. Thank you. "Print out some spaces over the top of the bit you want to erase"

CodePudding user response:

You can do something like that:

import time

def disappearingPrint(message, interval):
  for i in range(0, len(message)):
    print(message[:len(message)-i] " "*i, end="\r")
    time.sleep(interval)

disappearingPrint("Hello world", 1)

I'm adding spaces every time one character is removed

  • Related