Skip to content
← Back to Community
Making a Slowtype function in python
Profile icon
[deleted]

Slowtyping in Python

I'm sure you've seen projects that print out strings slowly instead of all at once. Today i'll show you how. Here is the code:

def slowtype(str): newstr = "" strcount = 0 clearcheck = 0 for element in str: strcount = strcount + 1 for element in str: newstr = newstr + element print(newstr) time.sleep(0.05) clearcheck = clearcheck + 1 if(strcount == clearcheck): break else: os.system('clear')

Lets brake it down:

first, the function accepts a string. Then, it makes 2 integers. strcount, which measures the length of the string, and clearcheck, which helps later. To figure out how long the string is, it iterates over each character in a string. Then, it moves to the second loop where the loop adds each character to a string, prints the string, increases clearcheck by 1, sleeps for 0.05 seconds, then checks if strcount is equal to clearcheck. If it is, it doesn't clear the screen. If it isn't, it clears and repeats. This used as a function will give a nice slowtype function to python code!

Here's an example:

Voters
Profile icon
Aroxret
Profile icon
frissyn
Profile icon
heavyduck
Profile icon
TigerTheCat
Comments
hotnewtop
Profile icon
Bookie0

easier way with less code:

import sys, os # imports import time st = 0.04 def sp(str): for letter in str: # for loop to loop through each char sys.stdout.write(letter) sys.stdout.flush() time.sleep(st) # waits a certain amount of time print()

I usually put st at 0.04 because it prints it not too fast and not too slow. But you can change it. Just change the value to what you want after st =

Each time you want to do this typewriter effect, just do:

sp(“Hello World”)

Output:

Hello World

(Outputted one char at a time with a break of 0.04 seconds between each letter) :)

Profile icon
[deleted]

@Bookie0 Nice! I'll have to try that sometime

Profile icon
Bookie0

ye maybe change the post then lol? :D @Kronifer

Profile icon
RahulChoubey1

@Bookie0 Uhh… You can replace this:

sys.stdout.write(letter) sys.stdout.flush()

with this:

print(letter, end = "", flush = True)

And it gets rid of the sys import. If you want to use the sys method, put the import within the function(because we may not need sys for the whole program).

Profile icon
[deleted]

@Bookie0 I'm ok, but i'll probably use that in something else I work on!

Profile icon
Bookie0

@RahulChoubey1 yea I put sys outside of program as I could use it for other stuff :)

Profile icon
fuzzyastrocat

@Bookie0 My version, with easier usage and scoping:

def sp(string, delay=False): if not delay: delay = sp.DELAY import time for letter in string: print(letter, end='', flush=True) time.sleep(delay) print() sp.DELAY = 0.04

Usage:

sp("Hello, world") # at 0.04s delay sp.DELAY = 0.3 sp("Hello, world") # at 0.3s delay sp("Hello, world", 0.1) # at 0.1s delay sp("Hello, world") # back to 0.3s delay

EDIT: Just noticing this is similar to what @RahulChoubey1 said. Keeping this around just for the scoping mechanism though.

Profile icon
Bookie0

@fuzzyastrocat yay the more versions the better lol :D

Profile icon
fuzzyastrocat

@Bookie0 With more diversity you can see which way you like best and then have a better scope of the issue. So yeah, definitely!

Profile icon
Bookie0
Profile icon
JasonLiu19

@Bookie0 My way:

import time def sp(str, delay): for v in str: print(v,end="",flush=True) time.sleep(delay) sp("Hello World!",.04)
Profile icon
Bookie0

@JasonLiu19 nice, but do you have to say ,.04 each time you call the function?

Profile icon
JasonLiu19

@Bookie0 No, it can be .5 or 199320, there has to be a number though

Profile icon
Bookie0

@JasonLiu19 yea, but you have to include a number each time no?

Profile icon
JasonLiu19

@Bookie0 if you don't want that, just do this:

def sppof(str): for v in str: print(v,end="",flush=True) time.sleep(.04)

also my syntax highlighting

6f8d0dbc0e444b924fbebbb89c1a4cd3

Profile icon
JasonLiu19
Profile icon
Bookie0

@JasonLiu19 yeye ik lol I was just wondering if you knew about it lol

Profile icon
RahulChoubey1

@fuzzyastrocat So you can set the default too. Noice

Profile icon
JosephWren

@Bookie0 I know this is a bit of time after you posted this but whenever I ask for an input from the user:

a = float(input(write("What is your a? ")))

it puts 'None' at the end, how do I fix this?

Profile icon
Deltonium

Can't you just use len(str) instead of the for loop?

Profile icon
[deleted]

@Deltonium lol yeah you could, I didn't think of thst

Profile icon
TigerTheCat

Wow, this is awesome! I've always wanted to learn how to do this!

Profile icon
[deleted]

@TigerTheCat No problem! Honestly, I kinda just set this up today and didn't think it would work :)

Profile icon
TigerTheCat

What do you mean, @Kronifer? It works great!

Profile icon
[deleted]

@TigerTheCat I set this up for a project this morning, took some troubleshooting. Also, thanks!

Profile icon
TigerTheCat

No problem, @Kronifer! You really deserve more cycles, I don't know why nobody else is giving you any!

Profile icon
NotNoemie

one liner method (2 lines including imports)
from sys import stdout; from time import sleep
for letter in text: stdout.write(letter); stdout.flush(); sleep(0.1)

Profile icon
frissyn

@Bookie0 & @Kronifer

This uses significantly less code, keeps time limited to the function's namespace, and uses milliseconds instead of seconds, since you'll very rarely need to pause between each character for more than a single second. (It also uses typing, which is always really cool).

def typewriter(value: str, stutter: int): import time for char in value: print(char, end="", flush=True) time.sleep(stutter/1000.0) print(flush=True)

:)

Profile icon
Bookie0

you're alive!!

yea I use seconds because with milliseconds there's just a bit more 0s and ya know the less the better, and I also sometimes pause between each char for more than a single second for suuuuper long texts to annoy the users lol

but yea I also import time at the top because I sometimes use it not only in the function but in the rest of the program as well! :D

:))))

@IreTheKID

Profile icon
frissyn

just a suggestion :)

Profile icon
[deleted]

Seems my post has caused a large conversation lol 🤣

Profile icon
Bookie0

add syntax highlighting to your code blocks by adding py after the 3 backslashes (`):

def slowtype(str): newstr = "" strcount = 0 clearcheck = 0 for element in str: strcount = strcount + 1 for element in str: newstr = newstr + element print(newstr) time.sleep(0.05) clearcheck = clearcheck + 1 if(strcount == clearcheck): break else: os.system('clear')

Screen Shot 2020-12-02 at 1.53.59 PM

;)

Profile icon
JBloves27

Its pretty cool, but umm, i would add more effort. Like adding colors? or add more to the tutorial? bc its rly plain. no offense

Profile icon
[deleted]

@JBYT27 Yeah, that sounds like a good idea!

Profile icon
JBloves27