Printing a string with a little delay between the chars

前端 未结 7 1577
青春惊慌失措
青春惊慌失措 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-20 00:08

    You are printing the result of time.sleep(0.2), which is None. Move it to the next line.

    text = "Hello, this is a test text to see if all works fine."
    for char in text:
        print char,
        time.sleep(0.2)
    

    Of course, you still have the problem of a space between each character, which can be solved by replacing the print statement with a call to sys.stdout.write.

    text = "Hello, this is a test text to see if all works fine."
    for char in text:
        sys.stdout.write(char)
        time.sleep(0.2)
    
    0 讨论(0)
提交回复
热议问题