-1

I have devised the following code for delaying the printing of a sentence character by character in VS Code. Here's the code for the program:-

import time

sentence = "Whats Up My buddies!"
start = time.time()
for character in sentence:
    print(character, end="")
    time.sleep(0.6)
end = time.time()
print("Elapsed time:", end - start, "seconds")

I tried to time my code using time.time() and the result I was getting was about 12 seconds. I also checked with another code editor but it also gave the same result.

I have expected that each character of the sentence would be printed out slowly one after another with a 0.6 second delay, but the whole sentence was printed out immediately after 12 seconds instead.

Claudio
  • 7,474
  • 3
  • 18
  • 48
  • 3
    `len(sentence)` is `20`. Sleeping for 0.6 seconds 20 times is 12 seconds. – Brian61354270 Feb 05 '23 at 16:18
  • You print about 20 characters with 0.6 second pauses, how is it surprising that this takes about 12 seconds?? – Thierry Lathuille Feb 05 '23 at 16:18
  • How long are you expecting the code to run? – gbeaven Feb 05 '23 at 16:22
  • 1
    I was expecting that each character would print out slowly one after one with a 0.6 second delay but it prints it out immediately after 12 seconds. – Rohan Banerjee Feb 05 '23 at 16:33
  • On my system each character is printed out slowly one after another with 0.6 seconds delay. Try to run the code from file in the console or in IDLE. – Claudio Feb 05 '23 at 16:44
  • 2
    This was absolutely not clear in your question: the total execution time is totally irrelevant, and you never mentionned this information, which is the only important one. Anyway, this is a duplicate of https://stackoverflow.com/questions/230751/how-can-i-flush-the-output-of-the-print-function , and you need to flush after printing each character. Use `print(character, end="", flush=True)`. – Thierry Lathuille Feb 05 '23 at 16:47
  • @RohanBanerjee : in order to mark this question as answered it is necessary to accept the answer. – Claudio Feb 05 '23 at 17:29
  • Okay I will keep that in mind next time. Thank you for your help! – Rohan Banerjee Feb 05 '23 at 18:16

1 Answers1

0

Try:

import time
import sys
sentence = "Whats Up My buddies!"
start = time.time()
for character in sentence:
    print(character, end="")
    sys.stdout.flush()
    # or as mentioned by Thierry Lathuille in the comments:   
    # print(character, end="", flush=True)
    time.sleep(0.6)
end = time.time()
print()
print("Elapsed time:", end - start, "seconds")

and check out: Usage of sys.stdout.flush() method

The problem you run into is that the stdout output of print() can be buffered. If it is buffered in the Python environment you run the code you will experience the effect you have observed because the buffer will be filled first with the characters and then the entire sentence will be written to stdout at once.

The solution is to use unbuffered stdout or force the system to flush the buffer what you can do using sys.stdout.flush() method.

On my system running your code in SciTE editor gives slow typing effect. Same if I run the code stored in a file in IDLE.

But running your code in the terminal from the file or the interactive Python prompt gives me the whole sentence at once.

Claudio
  • 7,474
  • 3
  • 18
  • 48