Cool text effects

Hey guys! Have you ever wondered how people can make a typewriter-style effect when printing text? Or text with colour?
Well, we’ll learn that now!

Scrolling Text

We’ll cover this first. To create this effect, we’ll use the sys and time libraries in Python.

import sys, time

Then, we’ll make a for loop.

for char in string:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.05)

But hey! Nothing runs! Well, we need to specify what string to type. To do this, we can put the for loop in a function.

def scroll(string):
    for char in string:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.05)

After this, we can input it in this way:

scroll("Hello")

There! That’s the typewriter effect!

Adding Colours

Colours are in this format:
\033[0m
The value here (0) can be changed for different effects.
Here’s the list:

BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
RESET = "\033[0m"

Here’s an example by using an f string (f strings allow variables to be easily inserted into a string by using curly braces ({})):

print(f"{BOLD}{BLUE}Hello, World!")

We can also add 2 effects at the same time:

print(f"{BLINK}{UNDERLINE}Hello, World!")

Using RGB color codes, you can do almost every color and background color that can be intrepreted by a computer. This is equal to more than a billion colors.

The format of RGB color codes are like this: \033[38;2;r;g;ba. If you would like to do a background, replace 38 with 48:\033[48;2;r;g;ba.


There! I hope this tutorial helped you!

Was this tutorial helpful?
  • Helpful
  • Not helpful
0 voters

If there is anything that can be improved/corrected, please tell me so that I can correct any mistakes that I might have made!

2 Likes

@NateDhaliwal great tutorial, but could you fully right out the colors? Like so

BOLD = "\033[1m"

# or

PINK  = "\033[32m"

Because you can then do this,

print(f"{BOLD}{PINK}Hello World!") # the text is pink and bold

Here is the full list:

BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
RESET = "\033[0m"

Also could you make this topic a Wiki so we can add in info or change it?


P.S the scroll’s wait is incredibly long (over a tenth of a second) I usually do 0.05 seconds you get the effect but it doesn’t take forever to write it out.

2 Likes

Just a note: the typewriter effect also applies to the color codes, which you can’t see. So if it seems like it’s not doing anything, it’s probably printing the color codes.

2 Likes