Learn to Code via Tutorials on Repl.it!

← Back to all posts
Typewriter effect - Python
stoporinjail (136)

Ever think that the

print("Hello")

is too sudden?

Well today I can help with that.
I will be showing you how to make a typewriter, or typing effect!

Modules required

import time
import sys

We will create a function to do the effect, so we do:

def write(write):

Make sure you do the (write) inside of the brackets, it s important.

Now we do the code.

def write(write):
    # Repeats for each letter.
    for i in write:
        # sys.stdout.write doesn't create a new line for each print
        sys.stdout.write(i)
        sys.stdout.flush()
        time.sleep(.05)
    # Do you want to continue to the next text?
    next = input()

Alternativeley, you can do a time.sleep replacing next = input()
However, if you do this, you have to add in

print("")

to create a new line.

Now to print it

write("Text here")

or

write = "text"
write(write)

It is recommended you do the first option

Conclusion

I hope you found out something new today, bye!

Comments
hotnewtop
DynamicSquid (4932)

You can remove the newline at the end of print

def write(s):
  for i in s:
    print(i, end="", flush=True)
    time.sleep(.086)