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:

You are viewing a single comment. View All
frissyn (403)

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

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

frissyn (403)

just a suggestion :)