I have the following loop which prints a period after each iteration:
for i in range(3):
time.sleep(1)
print('.')
This runs exactly as expected: a period is printed to the terminal on a new line in 1 second increments
. # after 1 seconds
. # after 2 seconds
. # after 3 seconds
I want to change the output to print periods to the same line (so the output is more compact and easier to read). However, I have a problem when I change the code to the following:
for i in range(3):
time.sleep(1)
print('.', end='')
In this case, the single line of periods only print all at once when the for loop is complete:
... # all after 3 seconds
Am I doing something wrong here or there a limitation with the python print statement?
printby default operate on each line, if you want it to generate dynamic content like., then... You'll need to usesys.stdout.flush()