Writing a Cool Happy Birthday Text Using Python

Handhika Yanuar Pratama
4 min readJun 1, 2024

--

Photo by Annie Spratt on Unsplash

June is the month when my crush celebrates her birthday; I hope she doesn’t see this because I want to surprise her with stories. Python is one of the great languages I’ve been learning for a few years. It’s a very convenient programming language; all we need are ideas to make things work.

The Modules

In this stories, let me write about how to write cool happy birthday text, but first of all we should need two modules installed, it’s pyfiglet and termcolor. Both of them is used for different cases. Pyfiglet is used to generate beautiful text and the termcolor is used for the coloring styles.

Look at here when I want to run the program it ask me to install pyfiglet

And yeah let’s install pyfiglet.

Pyfiglet is solved, but it need termcolor installed, let’s installed it too

The command is very simple like this.

And finally the program is working.

The Program

Well, the above program in The Module section is static, I want it to be blink, so it could be attractful to see.

One way to achieve blinking text in Windows cmd is by using ctypes library in Python to call the Windows API function SetConsoleTextAttribute to change the text attributes (including blinking) of the console. Here is the full source code

import pyfiglet
import random
import time
import os

def clear_screen():
# Function to clear the screen in cmd
os.system('cls' if os.name == 'nt' else 'clear')

def happy_birthday():
colors = ['red', 'green', 'blue', 'yellow', 'magenta','rose', 'cyan', 'white']
ascii_art = pyfiglet.figlet_format("Happy Birthday, my love", font="slant")
while True:
for color in colors:
# Print the text in different colors to create blinking effect
clear_screen()
print(pyfiglet.figlet_format("Happy Birthday, my love", font="slant", justify="center"))
print("\033[{}m".format(random.randint(31, 37)), end='')
time.sleep(0.5)
clear_screen()
time.sleep(0.5)

happy_birthday()

The Breakdown

The script uses a module called pyfiglet to create ASCII art text, making text look fancy. It also uses the random module to pick colors randomly and the time module to control the timing of the blinking effect. The os module is used to clear the screen for a smoother visual effect.

import pyfiglet
import random
import time
import os

A function called clear_screen() is defined to clear the screen in the command prompt.

def clear_screen():
# Function to clear the screen in cmd
os.system('cls' if os.name == 'nt' else 'clear')

Another function, happy_birthday(), is defined to display the birthday message and make it blink.Inside happy_birthday():

  • A list of colors is created.
colors = ['red', 'green', 'blue', 'yellow', 'magenta','rose', 'cyan', 'white']
  • The ASCII art for “Happy Birthday” is generated using pyfiglet.
  • The program enters an infinite loop.
ascii_art = pyfiglet.figlet_format("Happy Birthday, my love", font="slant")
  • Inside the happy_birthday() we generate looping that iterate through the color from the lists, the fist was calling the clear_screen() function.
while True:
for color in colors:
# Print the text in different colors to create blinking effect
clear_screen()
  • The birthday message is printed in ASCII art at the center of the screen.
print(pyfiglet.figlet_format("Happy Birthday, my love", font="slant", justify="center"))
  • The text color is set to a random color.
print("\033[{}m".format(random.randint(31, 37)), end='')
  • The program waits for a short time. The screen is cleared again. Next, another short pause occurs before the loop repeats, creating a blinking effect.
time.sleep(0.5)
clear_screen()
time.sleep(0.5)

Here are the results after we run the program.

Conclusions

Well, the text could be anything you want, it just based on your own idea, in this stories, since my crush is getting birthday, I like to write this article, hehe. Thanks for reading. 👧💝

--

--

Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer