Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

python - How to print a string with a little delay between the chars?

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 = "Hello, this is a test text to see if all works fine."
for char in text:
   print char,time.sleep(0.2),

It works fine, except for one problem. I get a "None" after every character.

This is the output:

H None e None l None l None o None , None   None t None h None i None s None   None i None s None   None a None   None t None e None s None t None   None t None e None x None t None   None t None o None   None s None e None e None   None i None f None   None a None l None l None   None w None o None r None k None s None   None f None i None n None e None . None

I don't know why this happens. I hope anyone can help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
>>> import time
>>> import sys
>>> blah = "This is written slowly
"
>>> for l in blah:
...   sys.stdout.write(l)
...   sys.stdout.flush()
...   time.sleep(0.2)
...
This is written slowly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...