Skip to content
← Back to Community
scroll text
Profile icon
DiegoCamacho3

hey does anyone know how to make a script for scroll text in python? if anyone does please let me know (only if you agree to the idea of me stealing your code). thanks. =]

Voters
Profile icon
DominicFranklin
Profile icon
Kitty6709
Profile icon
DiegoCamacho3
Comments
hotnewtop
Profile icon
Bookie0

from my tutorial here:

Now this typewriter effect is super cool, because it makes the text print one letter at the time. You can regulate it so that it prints the text fast or slow.
First the imports then the code in a function:

import sys, os import time st = 0.04 def sp(str): for letter in str: sys.stdout.write(letter) sys.stdout.flush() time.sleep(st) print()

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

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

sp(“Hello World”)

Output:

Hello World
Profile icon
EthanDoyle2

@Bookie0 hi, ive been using this and its pretty good. however, whenever i use this for an input, it immediately replies with "None" (i'll paste some code below to show you what i mean) - any help would be greatly appreciated.

import sys import time st = 0.04 def sp(str): for letter in str: sys.stdout.write(letter) sys.stdout.flush() time.sleep(st) print() sp("Hello.") time.sleep(1) name = input(sp("What is your name?")) sp(f"Thank you {name}")
Profile icon
Coder100
import time def typewriter(text, delay=0.1): for letter in text: print(letter, end='', flush=True) time.sleep(delay) print() typewriter("I'm being typed at a rate of 0.1 seconds") typewriter("I'm being typed at a rate of 0.05 seconds", 0.05)

oh and btw, for user input, try this function:

import time def typewriterinput(text, delay=0.1): for letter in text: print(letter, end='', flush=True) time.sleep(delay) print() return input() print("you said %s" % (typewriterinput("say something: ")))
Profile icon
DiegoCamacho3

@Coder100

when i try using a variable for the input for example if i ask them their name, and then use a typewriter statement, it gives me an error

Profile icon
GulliverM

@DiegoCamacho3 you forgot the %'s, also i recommend doing it like this:

typewriter(f"you typed: {input("type something")}!")

also I overwrite the print function and make it accept any amount of arguments, have a separator, and an end:

def print(*data, interval=0.04, sep=None, end=None): import sys, time for i in data: for x in i: sys.stdout.write(x) time.sleep(interval) sys.stdout.flush() if sep != None: print(sep) if end != None: print(end) # so that i can use normal syntax: print("hi, " input("what is your name?"), "!") # it works!

this way, I can add it to old programs, won't have to switch between syntax, and it i don't want it, can just comment it out

thank you for your time.

Profile icon
Coder100

make sure to copy all of the text, but use it like an input() statement. @DiegoCamacho3

Profile icon
RYANTADIPARTHI

Try this:

import sys import time for x in ("WELCOME"): sys.stdout.write(x) sys.stdout.flush() time.sleep(0.05)

That should work

Profile icon
DiegoCamacho3

@RYANTADIPARTHI

it didnt work

Profile icon
RYANTADIPARTHI

@DiegoCamacho3 did it at least type welcome? Because if you are using a like scrollTxt() it won't work

Profile icon
MocaCDeveloper

@RYANTADIPARTHI

You forgot something lol.
I think having the time module imported is key to using time.sleep

Profile icon
RYANTADIPARTHI

@MocaCDeveloper ah yes, thanks for reminding me.

Profile icon
MocaCDeveloper

@RYANTADIPARTHI

Yup figured it would help out :)

Profile icon
RYANTADIPARTHI
Profile icon
Bookie0

@RYANTADIPARTHI put it in a function

Profile icon
MocaCDeveloper

Scroll text?

Profile icon
DiegoCamacho3

@MocaCDeveloper yes, text that doesn't go immediately like the default one, but one character at a time.

Profile icon
MocaCDeveloper

@DiegoCamacho3

Oh. Hmm.

import sys from time import sleep str_ = input("Something: ") for i in str_: sys.stdout.write(i) sys.stdout.flush() sleep(.25)

^^ that should work :D

Profile icon
DiegoCamacho3

@MocaCDeveloper ok ill try it out

Profile icon
MocaCDeveloper

@DiegoCamacho3

Aright let me know if it works!

Profile icon
DiegoCamacho3

@MocaCDeveloper

it does work but is there a prefix to change the print statement? so for example:
scrollTxt("something")

so basically like the scroll text replaces the print statement.

Profile icon
MocaCDeveloper

@DiegoCamacho3

No I believe not. I believe this is the only way :)

Profile icon
MocaCDeveloper

@DiegoCamacho3

I mean. you can make a function yourself that contains the code to make the "scroll text" affect:

import sys from time import sleep def scrollTxt(text,seconds = .45): for i in text: sys.stdout.write(i) sys.stdout.flush() sleep(seconds) scrollTxt('yo dude',1) // prints each character every other second
Profile icon
DiegoCamacho3
Profile icon
MocaCDeveloper

@DiegoCamacho3

Did it help out?