Caelan Grgurovic Caelan Grgurovic - 3 months ago 5
Python Question

replace previous print output with new print output or add characters to previous output

I'm pretty new to Python programming, and I'm attempting to create a simple little game with a little "initialization" on boot up.

I pretty much want it to spell out "Welcome", but with all of the letters popping up every 100ms.

I've got the

sleep
function down pat, however, rather than adding on each character to the end of the previous line, it is simply printing it all out on a new line.

This is what I'm currently getting:

W
e
l
c
o
m
e
.


I'd prefer it to just be
Welcome.


This is my current code:

print("W")
time.sleep(0.1)
print("e")
time.sleep(0.1)
print("l")
time.sleep(0.1)
print("c")
time.sleep(0.1)
print("o")
time.sleep(0.1)
print("m")
time.sleep(0.1)
print("e")


I have looked around, but have failed to understand how to implement the function into Python. So an explanation would be extremely appreciated!

Thanks.

Answer

Use end='' when calling print():

import time
for c in "Welcome":
    print(c, end='')
    time.sleep(0.1)
Comments