14

I want to make such program which reads characters from a string and prints each character after some delay so its look like typing effect.

Now my problem is sleep function is not working properly. It print whole sentence after long delay.

import sys
from time import sleep

words = "This is just a test :P"
for char in words:
    sleep(0.5)
    sys.stdout.write(char)

I use "sys.stdout.write" for removing whitespace between characters.

Julian E.
  • 4,687
  • 6
  • 32
  • 49
Parth Gajjar
  • 1,334
  • 3
  • 12
  • 35

7 Answers7

19

you should use sys.stdout.flush() after each iteration

The problem is that stdout is flushed with the newline or manually with sys.stdout.flush()

So the result is

import sys
from time import sleep

words = "This is just a test :P"
for char in words:
    sleep(0.5)
    sys.stdout.write(char)
    sys.stdout.flush()

The reason why your output is buffered is that system call needs to be performed in order to do an output, system calls are expensive and time consuming (because of the context switch, etc). Therefore user space libraries try to buffer it and you need to flush it manually if needed.

Just for the sake of completeness ... Error output is usually non-buffered (it would be difficult for debugging). So following would also work. It is just important to realise that it is printed to the error output.

import sys
from time import sleep

words = "This is just a test :P"
for char in words:
    sleep(0.5)
    sys.stderr.write(char)
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
10

In python 3, you can replace the calls to sys.stdout with standard print calls:

for char in words:
    sleep(0.1)
    print(char, end='', flush=True)
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
2

You have to flush the stdout at each loop, to empty the buffer:

import sys

from time import sleep

words = "This is just a test :P\n"
for char in words:
    sleep(0.5)
    sys.stdout.write(char)
    sys.stdout.flush()

Without this, it just stored your string in the buffer and wait for an \n (or sufficient amount of characters to print), which come at the end of your loop....

More info :

Community
  • 1
  • 1
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
0
import sys
from time import sleep

words = "Welcome to CB#SA.NET.CO"
for char in words:
sleep(0.1)
sys.stdout.write(char)
sys.stdout.flush()
Robert
  • 5,278
  • 43
  • 65
  • 115
0

Long Answer:

import sys
import time

variable_name = 'Words'

for char in variable_name:
    time.sleep(0)
    sys.stdout.write(char)

Why do you need the flush method? My code works without it.

Short Answer:

import time

variable_name = 'Words'

for char in variable_name:
    time.sleep(0)
    print(char, end = '')

If you want the words to print vertically, then you should leave out the end argument.

If you want the words to print horizontally, then you should use the end argument.

Either way, the code works without the flush method.

0

To make it easier, You should put a sys.stdout.flush and a def function. For example:

    import sys
    import time

    def print_slow(str):
        for char in str:
            time.sleep(.1)
            sys.stdout.write(char)
            sys.stdout.flush()

print_slow('Hello')

This should work because I have tested it. The def makes it easier to write out your strings and you can custom it anyway you like. (:

liil
  • 37
  • 4
0

I have a similar answer to @Jon Vorcak,

we use a function, with his code

#you can also call it whatever you want
def type(text):
  words = text
  for char in words:
    time.sleep(0.2)
    sys.stdout.write(char)
    sys.stdout.flush()
type("hi")