Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how do i make the python test output seem as if it is being typed out or printed slower

+8 votes
asked Feb 5 by Joshan Chana (200 points)

1 Answer

+1 vote
answered Feb 5 by Peter Minarik (101,420 points)
edited Feb 9 by Peter Minarik

A typical solution could be to delay the printing out of characters in a text. You can go with a fixed delay (0.1 seconds in my example below), or you can randomize the delay (e.g., between 0.05 and 0.2 seconds), or assign a specific delay for specific cases.

It's important to note for the below example that one must flush the standard output, otherwise nothing will be printed until the whole text is done, as Python tries to print when its internal buffer is ready, instead of printing individual characters (for performance reasons).

This should work:

import time;
import sys;

text = "The man in black fled across the desert and the gunslinger followed."

for ch in text:
    time.sleep(0.1)
    sys.stdout.write(ch)
    sys.stdout.flush()
commented Feb 10 by Samuel Castre SigaAn (100 points)
import time
import sys

def slow_print(text, delay=0.05):
    for char in text:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(delay)
    print()  # newline at the end

slow_print("Hello there... testing dramatic output ", 0.04)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...