Wish Happy Birthday using Python Programs - PySeek (2024)

Wish Happy Birthday using Python Programs - PySeek (1)

Table of Contents

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 captivating 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 timefrom random import randintfor 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

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 instance, the message will be presented in vibrant ASCII text. To achieve this effect, we will incorporate 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.

fontsDownload

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 timeimport timeimport randomimport pyfiglet as pffrom pyfiglet import Figletfrom termcolor import colored# Write your message hereText = "Happy Birthday"# Enter his/her nameName = ""# Reverse your given namereverseText = 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

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 incorporates the ‘pyfiglet’ and ‘termcolor’ modules to display a vibrant 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!

Wish Happy Birthday using Python Programs - PySeek (2024)
Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 5459

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.