Wish Happy Birthday using Python Programs

A 'Happy Birthday' text surrounded by colorful graphics, with a Python logo displayed in the top right corner.

Introduction

Birthday is one of the happiest days in the life of every human being. Everyone hopes that his/her loved ones will greet him/her uniquely on this day. Some give a sudden surprise, and some people wish by eye-catching gifts. But these are common modes of expression, right? As a Python Programmer, a creative idea sparked in my mind: What if we could extend our well-wishes through a Python program, adding a touch of uniqueness to birthday celebrations?

In this Python tutorial, I will show how you can wish someone Happy Birthday using Python Programs. Weā€™ll explore two distinct approaches to developing these programs.

The first program is pretty simple and requires no additional installations. Just run the script, and you can congratulate someone on their special day. To stop the program, you need to press ctrl+C.

The second program, however, introduces a more intriguing element. Here, we will utilize the pyfiglet and termcolor modules to create a unique Python program that distinctively expresses Happy Birthday. This tutorial will walk you through both methods.

So, without further ado, letā€™s embark on this coding journey.

Happy Birthday Program 1

Code

import time
from random import randint

for i in range(1,85):
    print('')
 
space = ''
for i in range(1,1000):
    count = randint(1, 100)
    while(count > 0):
        space += ' '
        count -= 1
    if(i%10==0):
        print(space + 'šŸŽ‚Happy Birthday!')
    elif(i%9 == 0):
        print(space + "šŸŽ‚")
    elif(i%5==0):
        print(space +"šŸ’›")
    elif(i%8==0):
        print(space + "šŸŽ‰")
    elif(i%7==0):
        print(space + "šŸ«")
    elif(i%6==0):
        print(space + "Happy Birthday!šŸ’–")
    else:
        print(space + "šŸ”ø")
    space = ''
    time.sleep(0.2)

Explanation of the above Program

The basic idea behind this program is to manipulate random numbers. Letā€™s break down how it works.

In the program, Iā€™ve set up a for loop for integer numbers ranging from 1 to 85, but it doesnā€™t print anything. Then, thereā€™s another for loop with numbers ranging from 1 to 1000. In each iteration, the program generates a random number between 1 and 100, storing it in the `count` variable. The program then prints spaces until the `count` variable reaches zero.

Following that, the program prints emojis, text, or both based on the modulus result (if `i % number` equals zero, where ā€˜numberā€™ is 10, 9, 8, 7, or 6). Finally, for each iteration, the program introduces a delay of 0.2 seconds.

Output

Output of Happy birthday program 1
Output of Program 1

Happy Birthday Program 2

This time, the script will display the ā€˜Happy Birthdayā€™ message on the Terminal or Command Prompt, similar to the previous method. However, in this case, the message will be presented in colorful ASCII text. To achieve this effect, we will use the pyfiglet and termcolor modules. A dedicated tutorial on these modules is available separately; you can access it here: [Wish Your Friends with Stylish Text in Python].

As these modules are third-party additions, you will need to install them separately. Letā€™s proceed with the installation process.

Setting up the Environments

Install the pyfiglet and termcolor modules using the following commands:

Install pyfiglet

pip install pyfiglet

Install termcolor

pip install termcolor

Download `fonts.txt` file

The program randomly chooses a font from 419 options and does the same for colors. Instead of listing all the fonts directly in the program, which would be too much, Iā€™ve put the font names in a separate text file. The program needs this file to pick fonts randomly. You can get the file by clicking the Download button below.

Make sure to have the text file (ā€˜fonts.txtā€˜) in the same folder as the program file (ā€˜HappyBirthday-2.pyā€˜).

Code

This Python program comes with an additional feature. You can personalize it by entering the name of the person you want to wish Happy Birthday. Simply input their name into the `Name` variable as your preferences.

import time
import time
import random
import pyfiglet as pf
from pyfiglet import Figlet
from termcolor import colored

# Write your message here
Text = "Happy Birthday"
# Enter his/her name
Name = ""

# Reverse your given name
reverseText = Name[::-1]

colorList = ['red', 'green', 'yellow', 'blue']
timeInterval = [0.2, 0.3, 0.2, 0.4]

# Getting all the font types from 'font.txt'
# and storing into a list
#==================================
dataList = list()
with open('fonts.txt') as f:
    for line in f:
        dataList.append(line.strip())
#==================================

# This part of the code prints the message
# with different font types(fixed and randomly)
#==================================
for i in range(1,1000):
    if(i%10==0):
        textArt = pf.figlet_format(Text)
        print("n", textArt)
    elif(i%9 == 0):
        textArt = pf.figlet_format(Text, font="xsbook")
        print(textArt)
    elif(i%5==0):
        F = Figlet(font=random.choice(dataList))
        textArt = colored(F.renderText(Name), random.choice(colorList))
        print("n", textArt)
    elif(i%8==0):
        F = Figlet(font=random.choice(dataList))
        textArt = colored(F.renderText(Text), random.choice(colorList))
        print("n", textArt)
    elif(i%7==0):
        textArt = pf.figlet_format(Text, font=random.choice(dataList))
        print("n", textArt)
    elif(i%4==0):
        textArt = pf.figlet_format(reverseText, direction = "right-to-left")
        print("n", textArt)
    else:
        print("")

    time.sleep(random.choice(timeInterval))

Output

Output of Happy birthday program 2
Output of Program 2

Summary

In this article, we explored the idea of using Python programming to wish someone Happy Birthday uniquely. Here, we developed two Python programs to achieve this. The first program uses loops and random numbers to print fun birthday messages with emojis and text.

The second program uses the ā€˜pyfigletā€™ and ā€˜termcolorā€™ modules to display a colorful Birthday Message with the personā€™s name in ASCII text form on the Terminal or Command Prompt.

So, what are you waiting for? Get ready to unpack the power of Python to turn ordinary birthday wishes into extraordinary celebrations! Letā€™s surprise your loved ones with a unique and engaging experience.

For additional lovely Python topics, explore the dedicated page exclusively designed for Cool Python Programs. Below are a few instances to spark your interest.

šŸ‘‰Say I Love You in Python Code

šŸ‘‰Communicate with Your Friends Secretly using Python

šŸ‘‰Draw the Sketch of Lionel Messi using a Python Program

šŸ‘‰Create a Time Wasting Websites Blocker using Python

Happy coding, and even happier birthdays ahead!

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! Iā€™m Subhankar Rakshit, the brains behind PySeek. Iā€™m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles:Ā 194