Printing slowly (Simulate typing)

I am trying to make a text game in python. However, everything is going well, I would like to make a function that will allow me to print something on the terminal, but in fashion the hat looks like typing.

I currently have:

def print_slow(str): for letter in str: print letter, time.sleep(.1) print_slow("junk") 

Output:

 junk 

Is there a way to get rid of spaces between letters?

+7
python
source share
4 answers

In Python 2.x, you can use sys.stdout.write instead of print :

 for letter in str: sys.stdout.write(letter) time.sleep(.1) 

In Python 3.x, you can set the optional end argument to an empty string:

 print(letter, end='') 
+13
source share

This is my function like a real person type:

 import sys,time,random typing_speed = 50 #wpm def slow_type(t): for l in t: sys.stdout.write(l) sys.stdout.flush() time.sleep(random.random()*10.0/typing_speed) print '' 
+7
source share

Try the following:

 def print_slow(str): for letter in str: sys.stdout.write(letter) sys.stdout.flush() time.sleep(0.1) print_slow("Type whatever you want here") 
+4
source share

Try this:

 import sys,time def sprint(str): for c in str + '\n': sys.stdout.write(c) sys.stdout.flush() time.sleep(3./90) sprint('hello world') 
0
source share

All Articles