Search code examples
pythonconsoleformattingformatstring-formatting

Python: Print to one line with time delay between prints


I want to make (for fun) python print out 'LOADING...' to console. The twist is that I want to print it out letter by letter with sleep time between them of 0.1 seconds (ish). So far I did this:

from time import sleep
print('L') ; sleep(0.1)
print('O') ; sleep(0.1)
print('A') ; sleep(0.1)
etc...

However that prints it to separate lines each.

Also I cant just type print('LOADING...') since it will print instantaneously, not letter by letter with sleep(0.1) in between.

The example is trivial but it raises a more general question: Is it possible to print multiple strings to one line with other function being executed in between the string prints?


Solution

  • You can also simply try this

    from time import sleep
    loading = 'LOADING...'
    for i in range(10):
        print(loading[i], sep=' ', end=' ', flush=True); sleep(0.5)