0

Using time.sleep() method in Python, I've been trying to print strings in a same line. Result I want is printing five "." in a line one by one every second.

I've tried end='' with print line, but it appeared together 5 seconds later.

Here is my code.

import time

for i in range(5):
    print("." , end='')
    time.sleep(1)

I expect the output of "....." to be printed separately every second (during 5 seconds), but the actual output appeared altogether after 5 seconds.

Sorry for bad english but if you don't mind, please help me :)

avariant
  • 2,234
  • 5
  • 25
  • 33
Hunter
  • 19
  • 7

2 Answers2

2
import time
for i in range(5):
    print('.', end=' ', flush=True)
    time.sleep(1)
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
AnRedd
  • 156
  • 8
  • Thank you! Can I ask you more what "flush=True" means? – Hunter Jun 12 '19 at 10:43
  • [Print_funciton](https://docs.python.org/3.3/library/functions.html#print) , [FlushOut](https://stackoverflow.com/questions/230751/how-to-flush-output-of-print-function) – AnRedd Jun 13 '19 at 12:26
0

So,

try to change your code to this:

import time

for i in range(5):
    print("." , end=" ", flush=True)
    time.sleep(1)

And look here: https://stackoverflow.com/a/3249539/11578778

Docs: https://docs.python.org/3/library/functions.html#print


Best regards, Brhaka

Brhaka
  • 1,622
  • 3
  • 11
  • 31