Learn to Code via Tutorials on Repl.it!

← Back to all posts
TypeWriter
CodingEssence

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:

One module, actually

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

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

Next we will put this:

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:

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

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!

Voters
CmdDatos
LukeSivil
Sant600
NFadhlurrahman
Haku
decomp
NikitaAnbattul
Deku40
DECLANBARTUNEK
halo4936
Comments
hotnewtop
Cooli

Very helpful for the new game I am creating!

Cooli

@Cooli btw I gave you 100 upvotes

CodingEssence

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

Cooli

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

CodingEssence

@Cooli Lol fine

Ezyh

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

Cooli

@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

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

CodingEssence

@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

@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

CodingEssence
ruiwenge2

@codingjlu what about my flask tutorial?

codingjlu

@ruiwenge2 link pls?

codingjlu

@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

@codingjlu oh it’s in the hot section

codingjlu

@ruiwenge2 oooh nice

ruiwenge2

@codingjlu thanks! :)

CodingEssence

@ruiwenge2 is flask like HTML but in python?

codingjlu
codingjlu

@CodingEssence there's no HTML replacement :/

CodingEssence

@codingjlu but what is flask?

codingjlu

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

CodingEssence

@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

codingjlu

@CodingEssence lol don't forget const

CodingEssence

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

codingjlu
kannibalistic

@codingjlu i dunno

FlaminHotValdez

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

@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

@CodingEssence Exactly ;). basically instead of

def printt(string, delay = 0.05):

maybe do

def printt(string, delay = #DELAY_HERE):

CodingEssence

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

Cooli

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

CodinUser

Hahaha simple :)

soha0001

i like this

CodingEssence
soha0001
Aivoybia
GreenMan2

yo man very helpful

CodingEssence
CodingEssence

@GreenMan2 Upvote?

GreenMan2
CodingEssence
ningxiner0201

Thanks!

HTML-96-Expert

I am going to use this tutorial soon. thanks!

HTML-96-Expert

nice

lernisto

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

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

CodingEssence

@ChristianDowell maybe its your connection, but thanks!

[deleted]

@Cooli @CodingEssence bruh stop updooting each other

CodingEssence
[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

Cooli
[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

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

Cooli

@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

@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

Thanks @Aphmeta

Cooli

@Aphmeta btw how can you upvote repls?

Cooli

@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 :(

Cooli

@Aphmeta Oh...

[deleted]

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

jeex

I like it. Thanks.

CodingEssence

@jeex np!

JerryyYt

very hard

[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!

CodingEssence

@Aphmeta thanks!

Aivoybia

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

Have a great rest of your day! :D

CodingEssence

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

Aivoybia

@CodingEssence thanks lol

CodingEssence
Jacintohelder3

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

CodingEssence
minishxp

i mean yeh this is simple but cool

CodingEssence
Dunce

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

the output looks like this:

to fix that just change your code to this:

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

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

EDIT: I actually found a way to fix that:

Dunce
CodingEssence

Even better solution:

Dunce

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

Sreedeepta

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

JakeHu2020

Btw when the code says

the ): also gets interpreted as a comment.

CodingEssence

@JakeHu2020 oops sorry error ill fix that

CodingEssence

@JakeHu2020 does it show error?

JakeHu2020

@CodingEssence no not anymore

CodingEssence

@JakeHu2020 ok thanks for letting me know

JakeHu2020

@CodingEssence your welcome :)

donnelmathews

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

CodingEssence

@donnelmathews I'm glad you liked it!

CodingEssence
donnelmathews

@CodingEssence
What does Upvote mean?

CodingEssence

@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
CodingEssence

@donnelmathews So you understand?

donnelmathews

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

donnelmathews

@CodingEssence
Ya i understand i clicked that button

geofspot

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:

[deleted]

Oof I was used to doing this:

CodingEssence

@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)