Learn to Code via Tutorials on Repl.it!

← Back to all posts
TypeWriter
CodingEssence (136)

How to create a "TypeWriter"

Have you ever seen those programs, in which the output is "typed" out? Like, each letter is printed out one by one? If you have, and would want to know how to make it? Well you have ended up in the right place!

Ok, no more talking; let's get right to the code.

So first of all, we need to import the modules:

from time import sleep
One module, actually

Now, we need to define our function; I have named it printt(), but you can name it whatever you want:

def printt(string, delay):
  # Our code will go here
  # Our code will go here
  # Our code will go here
If you have noticed so far but don't know, the string in the brackets of our defined function, that is a parameter. It is uesd as a placeholder for whatever we qant variables/string/numbers. Same for the delay. Delay pauses the print function for a certain amount of time so it makes it look like a "typewriter"

Ok, so far, we have our modules imported and our function defined. Now we can actually start coding.

So, we need a for loop to print out every letter.

I will be using the letter i for every character in the string and string as the string/variable to be "typed" out

def printt(string, delay):
  for i in str(string):
      # Our code will go here
      # Our code will go here
      # Our code will go here      

Next we will put this:

def printt(string, delay):
  for i in str(string):
    print(i, end="", flush = True)
    sleep(delay)

If you're stumped as to what end, and flush are, they are actually parameters for the print function. While you're coding, hover your mouse over the print function and it will show this:

These parameters are actually optional, so you won't need to put them to use the print function.
We put the end parameter there without anything in the quotation marks to indicate there is no end, so it keeps on printing from the line before, and flush, I honestly dont understand it still.

EDIT: flush is when you print something to the console text gets built up over time, but the new text isn't constantly rendered on the screen, instead its regularly "flushed" to the screen.
But printt() prints stuff to the console faster than it can render text, so by putting flush = True in your print statement you're forcing it to render the text in time.

Thanks to @Dunce for heling me out for flush

The sleep() function is from the time module, and is used to "pause" the code for a few seconds. Here, sleep() "pauses" the program for 0.05 seconds.

So far we have this:

from time import sleep
def printt(string, delay):
  for i in str(string):
    print(i, end="", flush = True)
    sleep(delay)

Next, we add a print() statement to print a new line after the string is typed out:

from time import sleep
def printt(string, delay):
  for i in str(string):
    print(i, end="", flush = True)
    sleep(delay)
  print('')
  # Not Inside the for loop, or it will mess up the program

AND WE ARE DONE!

That's it. I know its simple, but it's worth it as it makes the program less "dull"


Please check out my Racing Simulator

And be sure to check out my next tutorial: sep = ..., end = ..., and flush = ...


Please give me more ideas on what to code, I don't have no more ideas.


That's it for this Tutorial and see you in my next post; Bye!

Comments
hotnewtop
Cooli (21)

Very helpful for the new game I am creating!

Cooli (21)

@Cooli btw I gave you 100 upvotes

CodingEssence (136)

@Cooli Wow, thanks. I nearly had a heart-attack when you said 100 upvotes. im thinking, "199 cycles... what the-". lol

Cooli (21)

@CodingEssence Can you upvote me I only have 1, plz?

Ezyh (1)

@Cooli xD here's another upvote bro, have fun. also what game are you making?

Cooli (21)

@Ezyh idk my school is about to start in 3 weeks so I am just getting ready, currently trying to update my website! Here is the link: https://bit.ly/2VvLoid

codingjlu (484)

HOW did this get soooooo many votes? This is only 9 lines of code :(

CodingEssence (136)

@codingjlu lol that's what I'm saying: I go to bed and then the next day I wake up there's like 10 more upvotes

codingjlu (484)

@CodingEssence I don't know why :/ I feel the community is a bit... blah xD they can't tell what deserves to be upvoted and what doesn't

ruiwenge2 (1211)

@codingjlu what about my flask tutorial?

codingjlu (484)

@ruiwenge2 not that I'm trying to offend anyone. If I did, I'm sorry; I just think it's a little amazing that this tutorial could get so many votes!

ruiwenge2 (1211)

@codingjlu oh it’s in the hot section

CodingEssence (136)

@ruiwenge2 is flask like HTML but in python?

codingjlu (484)

@CodingEssence there's no HTML replacement :/

codingjlu (484)

@CodingEssence do you do Node.js? Flask is a web server library for Python.

CodingEssence (136)

@codingjlu Oh, that's what flask is, and no. I don't do Node.js. I used to know the basics... and forgot everything and only remember console.log() and var/let XD

CodingEssence (136)

@codingjlu I just remembered that as soon as you said it. oof

FlaminHotValdez (716)

Good post, it's nice to see a tutorial that isn't just a "Python basics" tutorial. But 1 little nitpick I have...

def printt(string, delay = 0.05):

Delay doesn't have to be 0.05, it can really be anything. The average reader reads around 250 WPM which is around 20 chars per second, which is good, but that does not account for people who are insane and read crazy fast and enjoy binge reading entire series over a single weekend who can read fast or people who read slow.

CodingEssence (136)

@FlaminHotValdez

who are insane and read crazy fast and enjoy binge reading entire series over a single weekend lol

I have had that in my mind while I was creating this; you can actually change the delay while you are coding bc it is "optional", meaning if you don't put any delay when you use that function, it will automatically default to 0.05, or if you did, it would default too that, or you could give an input at the very beginning of the code, which is what I might do in my next project. Thanks for reminding me about that

FlaminHotValdez (716)

@CodingEssence Exactly ;). basically instead of

def printt(string, delay = 0.05):

maybe do

def printt(string, delay = #DELAY_HERE):

CodingEssence (136)

@FlaminHotValdez Ok, I'll put that there; thanks!

Cooli (21)

@FlaminHotValdez I would prefer 0.03 as the delay if the text is large

amedida (0)

I am going to use this tutorial soon. thanks!

lernisto (0)

another idea: Use a different delay between each character.
You could

  • randomize the delay (this is easy and requires no storage, but is also not super-realistic: it is only a slight improvement over the fixed delay)
  • create character pairs and randomize the delay between each pair
  • record the delay from someone actually typing (biometrics: each person has a unique typing delay profile.)
ChristianDowell (0)

mine is buffering, I'll give you 1 upvote!

CodingEssence (136)

@ChristianDowell maybe its your connection, but thanks!

[deleted]

yay i got squid his 5000th cycle

[deleted]

@Cooli @CodingEssence bruh stop updooting each other

[deleted]

@CodingEssence yeah dude u have so many cycles from this one tutorial. u know what, ima gun for ur spot as the current top tutorial

[deleted]

@CodingEssence @Cooli you both realize u said the exact same thing? its like y'all are siblings... so stop LOLing everything. what the hack is replit going through.

CodingEssence (136)

@Aphmeta no we are not siblings I DO NOT know this person

Cooli (21)

@Aphmeta Okay I am going to stop "Laugh out loud"

[deleted]

@CodingEssence stop stop stop im joking im joking! (meme)

[deleted]

@Cooli hey cooli, u only have 3 cycles now, what happened?

Cooli (21)

@Aphmeta Maybe a guy unvoted me :(

[deleted]

@Cooli and updooted u right back ur at 4 now yay ima updoot every repl I find that you've posted so u can get more updoots since u really deserve them

Cooli (21)

@Aphmeta btw how can you upvote repls?

Cooli (21)

@Aphmeta Also I saw you bio thanks :D

[deleted]

@Cooli yeah if u check out my bio theres not much since im on a new account I deleted the last one my projects suck :(

[deleted]

@Cooli yeah im often bored so i play amogus then code but run out of ideas... BUT Im REALLY active on repl talk

[deleted]

mmm... croissant like

[deleted]

Any of y'all notice @CodingEssence just changed his bio from 12 year old to 13 year old? Well, I did, so happy (late) birthday!

Aivoybia (26)

Thank you so much! I added this to my project if you don't mind.

Have a great rest of your day! :D

CodingEssence (136)

@MEadphonesmead It's okay if you add it; it's meant to be used :P
And you also have a great tomorrow. lol

Jacintohelder3 (0)

Realy coll. That is my first week, I am liking realy, realy

seomind (0)

I adore your websites way of raising the awareness on your readers. Leptitox Review

seomind (0)

This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again. Leptitox

minishxp (7)

i mean yeh this is simple but cool

Dunce (71)

Nice! The only problem is that it doesn't add a newline at the end, so with the code:

printt("Hello", 0.05)
printt("world!", 0.05)

the output looks like this:

Helloworld!

to fix that just change your code to this:

from time import sleep

def printt(string, delay):
  for i in str(string) + "\n":
    print(i, end="", flush=True)
    sleep(delay)

Also you said you didn't understand flush, well when you print something to the console text gets built up over time, but the new text isn't constantly rendered on the screen, instead its regularly "flushed" to the screen.
But printt() prints stuff to the console faster than it can render text, so by putting "flush=True" in your print statement you're forcing it to render the text in time.

CodingEssence (136)

@Dunce Thanks!
Can i use this in the tutorial? I'll credit you

EDIT: I actually found a way to fix that:

def printt(string, delay = 0.05):
  LofS = len(string) - 1
  for j in string:
    if j != string[LofS]:  
      print(j, end="", flush = True)
      sleep(delay)
    elif j == string[LofS]:
      print(j)
CodingEssence (136)

Even better solution:

from time import sleep
def printt(string, delay):
  for i in str(string):
    print(i, end="", flush = True)
    sleep(delay)
  print('')
Dunce (71)

@CodingEssence It seems more readable, and easier to just manually add a newline character to me :)

Sreedeepta (1)

syntax error
can anyone make me understand the code clearly as I am a student of middle school pls anyone:]

JakeHu2020 (34)

Btw when the code says

def printt(string, delay = # Delay here):

the ): also gets interpreted as a comment.

donnelmathews (0)

Cooooooool!!!!!!!!!!!!!!!!!!!

CodingEssence (136)

@donnelmathews Oh, ok so you see this green button here:


that is the upvote button. For you, the button will be clear bc you haven't upvoted yet. Upvoting means that if you like the persons project, you can upvote it to show that you like it

donnelmathews (0)

@CodingEssence
Actually, I just clicked that button when I saw that image you send.😅

donnelmathews (0)

@CodingEssence
Ya i understand i clicked that button

geofspot (0)

Hey nice one there! Just joined the platform today and your post was the first I viewed, so I decided to add a little more fun to your code, see code below:

from time import sleep


def pytype ():
    user_string = input('Hey say something here:\n')
    user_delay = float(input('Specify delay time in float format, e:g = 0.05, 0.06 and etc:\n'))
    for chars in user_string:
        print(chars, end ='', flush = True)
        sleep(user_delay)
        
pytype()
Martin025 (3)

Oof I was used to doing this:

import time, sys, os
def printt(string):
  for letter in(string):
    sys.stdout.write(letter)
    time.sleep(0.03)
    sys.stdout.flush()
  print()
CodingEssence (136)

@Martin025 yea, i used to use that before, but someone else actually made it. So i decided to make my own (And more convenient way)

seomind (0)

I adore your websites way of raising the awareness on your readers. Zotrim Review

CodingEssence (136)

@seomind Could you please remove these messages or merge them in to one comment, as some of them are irrelevant. Thank you

seomind (0)

This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again. Zotrim