Learn to Code via Tutorials on Repl.it!

← Back to all posts
Making a Slowtype function in python
h
Kronifer (10)

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:

Commentshotnewtop
Bookie0 (4990)

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

Kronifer (10)

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

Bookie0 (4990)

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

RahulChoubey1 (89)

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

Kronifer (10)

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

Bookie0 (4990)

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

fuzzyastrocat (1462)

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

Bookie0 (4990)

@fuzzyastrocat yay the more versions the better lol :D

fuzzyastrocat (1462)

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

JasonLiu19 (68)

@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)
Bookie0 (4990)

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

JasonLiu19 (68)

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

Bookie0 (4990)

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

JasonLiu19 (68)

@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


Bookie0 (4990)

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

RahulChoubey1 (89)

@fuzzyastrocat So you can set the default too. Noice

Deltonium (1)

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

Kronifer (10)

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

TigerTheCat (5)

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

Kronifer (10)

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

TigerTheCat (5)

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

Kronifer (10)

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

TigerTheCat (5)

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

IreTheKID (373)

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

:)

Bookie0 (4990)

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

IreTheKID (373)

just a suggestion :)

Kronifer (10)

Seems my post has caused a large conversation lol 🤣

Bookie0 (4990)

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

;)

JBYT27 (1159)

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

Kronifer (10)

@JBYT27 Yeah, that sounds like a good idea!