0

I was helping a fellow in this website right here and then I noticed something very peculiar and quite specific, it is a very simple question but quite hard to explain, I will do my best to explain the problem.

I was doing this:

for i in range(100):
    print('h')

everything is fine, it will print out the letter 'h' 100 times very fast, and every time in a NEW LINE.

Then I was doing this: I want to print the letter 'h' slowly so I add time.sleep() inside the for loop

import time

for i in range(100):
    print('h')
    time.sleep(0.001)

And you know what, it works just fine! it is printing it slowly, every time in a NEW LINE

But I wanted it to print the letter 'h' in the same line without space between each 'h' like this: hhhhhh so I've changed the print from this: print('h') to this: print('h', end = '') in order to print the 'h' without a new line

and now when I am compiling the code with the new print:

import time

for i in range(100):
    print('h', end = '')
    time.sleep(0.001)

I can't see it printing the 'h' one by one, I can see all the 'h' letters only after the for loop has ended.

My question is:

  • Does anybody know why it happens?
  • why when I add end = '' to the print() it all the sudden acts differently.
  • Is this has anything to do with threads?
Dan
  • 144
  • 1
  • 8

0 Answers0