'How to replace console output when the string gets shorter?

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.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vale