Remove and Replace Printed items
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 Question
01:09 Accepted answer (Score 268)
01:24 Answer 2 (Score 140)
01:48 Answer 3 (Score 41)
02:15 Thank you
--
Full question
https://stackoverflow.com/questions/5290...
Answer 1 links:
[ANSI escape sequences]: http://en.wikipedia.org/wiki/ANSI_escape...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #printing #python32
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 Question
01:09 Accepted answer (Score 268)
01:24 Answer 2 (Score 140)
01:48 Answer 3 (Score 41)
02:15 Thank you
--
Full question
https://stackoverflow.com/questions/5290...
Answer 1 links:
[ANSI escape sequences]: http://en.wikipedia.org/wiki/ANSI_escape...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #printing #python32
#avk47
ACCEPTED ANSWER
Score 285
Just use CR to go to beginning of the line.
import time
for x in range (0,5):
b = "Loading" + "." * x
print (b, end="\r")
time.sleep(1)
ANSWER 2
Score 146
One way is to use ANSI escape sequences:
import sys
import time
for i in range(10):
print("Loading" + "." * i)
sys.stdout.write("\033[F") # Cursor up one line
time.sleep(1)
Also sometimes useful (for example if you print something shorter than before):
sys.stdout.write("\033[K") # Clear to the end of line
ANSWER 3
Score 42
import sys
import time
a = 0
for x in range (0,3):
a = a + 1
b = ("Loading" + "." * a)
# \r prints a carriage return first, so `b` is printed on top of the previous line.
sys.stdout.write('\r'+b)
time.sleep(0.5)
print (a)
Note that you might have to run sys.stdout.flush() right after sys.stdout.write('\r'+b) depending on which console you are doing the printing to have the results printed when requested without any buffering.