Ask coding questions

← Back to all posts
scroll text
DiegoCamacho3 (6)

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. =]

Comments
hotnewtop
alomax (0)

Prices offered by our website is cheap as compare to any other csgo account selling website
gta 5 modded accounts for sale
buy csgo accounts

Bookie0 (6371)

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
Coder100 (18904)
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: ")))
DiegoCamacho3 (6)

@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

Stan085 (23)

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

Coder100 (18904)

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

RYANTADIPARTHI (6020)

Try this:

import sys
import time

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

That should work

RYANTADIPARTHI (6020)

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

MocaCDeveloper (713)

@RYANTADIPARTHI

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

RYANTADIPARTHI (6020)

@MocaCDeveloper ah yes, thanks for reminding me.

MocaCDeveloper (713)

@RYANTADIPARTHI

Yup figured it would help out :)

DiegoCamacho3 (6)

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

MocaCDeveloper (713)

@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

MocaCDeveloper (713)

@DiegoCamacho3

Aright let me know if it works!

DiegoCamacho3 (6)

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

MocaCDeveloper (713)

@DiegoCamacho3

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

MocaCDeveloper (713)

@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