Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...