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:
Can't you just use len(str)
instead of the for loop?
@Deltonium lol yeah you could, I didn't think of thst
Wow, this is awesome! I've always wanted to learn how to do this!
@TigerTheCat No problem! Honestly, I kinda just set this up today and didn't think it would work :)
What do you mean, @Kronifer? It works great!
@TigerTheCat I set this up for a project this morning, took some troubleshooting. Also, thanks!
No problem, @Kronifer! You really deserve more cycles, I don't know why nobody else is giving you any!
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)
:)
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
:))))
just a suggestion :)
Seems my post has caused a large conversation lol 🤣
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')
;)
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
@JBYT27 Yeah, that sounds like a good idea!
easier way with less code:
I usually put
st
at0.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 afterst =
Each time you want to do this typewriter effect, just do:
Output:
(Outputted one char at a time with a break of
0.04
seconds between each letter) :)@Bookie0 Nice! I'll have to try that sometime
ye maybe change the post then lol? :D @Kronifer
@Bookie0 Uhh… You can replace this:
with this:
And it gets rid of the
sys
import. If you want to use thesys
method, put the import within the function(because we may not needsys
for the whole program).@Bookie0 I'm ok, but i'll probably use that in something else I work on!
@RahulChoubey1 yea I put
sys
outside of program as I could use it for other stuff :)@Bookie0 My version, with easier usage and scoping:
Usage:
EDIT: Just noticing this is similar to what @RahulChoubey1 said. Keeping this around just for the scoping mechanism though.
@fuzzyastrocat yay the more versions the better lol :D
@Bookie0 With more diversity you can see which way you like best and then have a better scope of the issue. So yeah, definitely!
@fuzzyastrocat ye :)
@Bookie0 My way:
@JasonLiu19 nice, but do you have to say
,.04
each time you call the function?@Bookie0 No, it can be
.5
or199320
, there has to be a number though@JasonLiu19 yea, but you have to include a number each time no?
@Bookie0 if you don't want that, just do this:
also my syntax highlighting
@Bookie0 yes
@JasonLiu19 yeye ik lol I was just wondering if you knew about it lol
@fuzzyastrocat So you can set the default too. Noice