0

I want to create a function which prints every character of a string with a small time break. I want them to print all the characters in the same line, so I use end. It is printing all of them in same line but there is a small delay in start and then it print all characters at once.

import time

def delay_print(a):
    for i in a:             
        print(i ,end = "")
        time.sleep(0.3)
    
delay_print("om")
solid.py
  • 2,782
  • 5
  • 23
  • 30
user14208629
  • 139
  • 1
  • 6

1 Answers1

3

The print call needs flush to be set to True, otherwise the output is buffered and printed at the end, as explained in this question.

import time

def delay_print(a): 
    for i in a:
        print(i, end = "", flush = True)
        time.sleep(0.3)
    print()

delay_print("om")

Output (After a second):

om
solid.py
  • 2,782
  • 5
  • 23
  • 30