Skip to content
← Back to Community
TypeWriter
Profile icon
CyberPy

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:

print(*values: object, sep: Optional[Text]=..., file: Optional[_Writer]=..., flush: bool=...) -> None

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!

Voters
Profile icon
DANGERBHAI
Profile icon
anonreaper
Profile icon
OrangeChromium
Profile icon
CyberPy
Profile icon
SwaritChoudhari
Profile icon
CaylixonBeta
Profile icon
sbzm
Profile icon
froggythefrog
Profile icon
levitannin
Profile icon
20Speedy12
Comments
hotnewtop
Profile icon
Cooli

Very helpful for the new game I am creating!

Profile icon
Cooli

@Cooli btw I gave you 100 upvotes

Profile icon
CyberPy

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

Profile icon
Cooli

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

Profile icon
CyberPy

@Cooli Lol fine

Profile icon
Ezyh

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

Profile icon
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

Profile icon
codingjlu

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

Profile icon
CyberPy

@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

Profile icon
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

Profile icon
CyberPy
Profile icon
ruiwenge2

@codingjlu what about my flask tutorial?

Profile icon
codingjlu

@ruiwenge2 link pls?

Profile icon
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!

Profile icon
ruiwenge2

@codingjlu oh it’s in the hot section

Profile icon
codingjlu

@ruiwenge2 oooh nice

Profile icon
ruiwenge2

@codingjlu thanks! :)

Profile icon
CyberPy

@ruiwenge2 is flask like HTML but in python?

Profile icon
codingjlu
Profile icon
codingjlu

@CodingEssence there's no HTML replacement :/

Profile icon
CyberPy

@codingjlu but what is flask?

Profile icon
codingjlu

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

Profile icon
CyberPy

@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

Profile icon
codingjlu

@CodingEssence lol don't forget const

Profile icon
CyberPy

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

Profile icon
codingjlu
Profile icon
kannibalistic

@codingjlu i dunno

Profile icon
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.

Profile icon
CyberPy

@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

Profile icon
FlaminHotValdez

@CodingEssence Exactly ;). basically instead of

def printt(string, delay = 0.05):

maybe do

def printt(string, delay = #DELAY_HERE):

Profile icon
CyberPy

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

Profile icon
Cooli

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

Profile icon
CodinUser

Hahaha simple :)

Profile icon
soha0001

i like this

Profile icon
CyberPy
Profile icon
soha0001
Profile icon
Aivoybia
Profile icon
GreenMan2

yo man very helpful

Profile icon
CyberPy
Profile icon
CyberPy

@GreenMan2 Upvote?

Profile icon
GreenMan2
Profile icon
CyberPy
Profile icon
kaylee402

Learning to program can be difficult, and students may require programming assistance. Many websites provide programming assignment help, and these websites provide useful tips and tactics to help students learn programming quickly.

Profile icon
CyberPy

No advertising please! @kaylee402

Profile icon
DavidSmith68

We offer assignment help Australia in almost every subject, thanks to our expert writers. Completing assignments is an important aspect for any student.

Profile icon
OrangeChromium

Is there a way to make this for input as well?

Profile icon
CyberPy

@OrangeChromium Yes!
You could do this:

def inputt(string, delay): for i in str(string): print(i, end="", flush = True) sleep(delay) return input('\n')

and you would use it like this:

your_variable = inputt('My String', 0.08)
Profile icon
heewoonkim2020

People who think this is just easy to make, this is not the case. This tutorial is very helpful to beginners starting to code in Python.

Profile icon
masonethan

Hi, I am Mason Ethan, a thesis proposal help expert. From the last 5 years I am providing thesis proposal writing help for the students, according to the review I am getting average 4.2 ratings for every student. If you also need help with your thesis proposal then you can visit our website. You can also connect me to the phone for instant response.

Profile icon
CaylixonBeta

This is very useful! Thank you

Profile icon
CyberPy
Profile icon
SenCode

It's nice. Honestly I didn't undestand flush() but now its OK. Thanks.

Profile icon
pipahaha

Typewriter and word counter are two tools I use often, it's great guys word counter online

Profile icon
AnaNolan

Thanks for sharing. It looks like a good thread with lots of information. I will bookmark this thread for further reference. In the meantime, I also need someone who can work for me as a plagiarism fixer. I have heard about Study Clerk in the past and now I want to give it a try here https://studyclerk.com/plagiarism-fixer What do you think? Will wait for your further reply.

Profile icon
ningxiner0201

Thanks!

Profile icon
HTML-96-Expert

I am going to use this tutorial soon. thanks!

Profile icon
HTML-96-Expert

nice

Profile icon
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.)
Profile icon
[deleted]

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

Profile icon
CyberPy

@ChristianDowell maybe its your connection, but thanks!

Profile icon
[deleted]

@Cooli @CodingEssence bruh stop updooting each other

Profile icon
CyberPy
Profile icon
[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

Profile icon
Cooli
Profile icon
[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.

Profile icon
CyberPy

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

Profile icon
Cooli

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

Profile icon
[deleted]

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

Profile icon
[deleted]

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

Profile icon
Cooli

@Aphmeta Maybe a guy unvoted me :(

Profile icon
[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

Profile icon
Cooli

Thanks @Aphmeta

Profile icon
Cooli

@Aphmeta btw how can you upvote repls?

Profile icon
Cooli

@Aphmeta Also I saw you bio thanks :D

Profile icon
[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 :(

Profile icon
Cooli

@Aphmeta Oh...

Profile icon
[deleted]

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

Profile icon
jeex

I like it. Thanks.

Profile icon
CyberPy

@jeex np!

Profile icon
JerryyYt

very hard

Profile icon
[deleted]

mmm... croissant like

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

Profile icon
CyberPy

@Aphmeta thanks!

Profile icon
[deleted]
Profile icon
Aivoybia

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

Have a great rest of your day! :D

Profile icon
CyberPy

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

Profile icon
Aivoybia

@CodingEssence thanks lol

Profile icon
CyberPy
Profile icon
Jacintohelder3

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

Profile icon
CyberPy
Profile icon
minishxp

i mean yeh this is simple but cool

Profile icon
CyberPy
Profile icon
Dunce

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.

Profile icon
CyberPy

@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)
Profile icon
Dunce
Profile icon
CyberPy

Even better solution:

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

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

Profile icon
Sreedeepta

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