I wanted to print 1 2 3 4 5
with 2 seconds of sleep gaps by using end
parameter for print()
.
import time
for i in range(5):
time.sleep(2)
print(i)
1
2
3
4
5
This code prints 1, 2, 3, 4, 5 line by line with 2 seconds of sleep gaps.
import time
for i in range(5):
time.sleep(2)
print(i, end=" ")
1 2 3 4 5 # It is printed at once after 10 seconds
However, when I use the end
parameter for print()
, it prints 1 2 3 4 5 at once after 10 seconds of sleep.
import time
for i in range(5):
time.sleep(2)
print(i, end=" ", flush=True)
1 2 3 4 5 # Each number is printed with 2 seconds of sleep as I expected.
Finally, I found a way to print 1 2 3 4 5 with 2 seconds of sleep gap each, by setting flush=True
.
Can anyone help me to understand how the end
causes this issue?
I really appreciate any help you can provide.