Printing a string with a little delay between the chars

前端 未结 7 1583
青春惊慌失措
青春惊慌失措 2020-12-19 23:20

I want a text to be displayed as if it is just being typed. So I need a little delay after every letter.

I tried to do it this way:

import time

text         


        
7条回答
  •  遥遥无期
    2020-12-19 23:51

    this line:

    print char, time.sleep(0.2)
    

    decodes as "print the value of char, and then print the return value of the function time.sleep() (which is None)".

    You can break them onto separate lines, but the default behavior of print followed by a comma will leave you with spaces between the characters that you probably don't want. If not, look up how to change the behavior of print, or do something like this:

    >>> import sys
    >>> import time
    >>> for char in "test string\n":
    ...    sys.stdout.write(char)
    ...    time.sleep(0.2)
    ...
    test string
    >>>
    

提交回复
热议问题