The shortest happy birthday code in Python is a single print() call with an f-string. Below you will find nine complete programs, from that one-liner to an ASCII-art banner, a text cake, a coloured and an animated terminal message, a Turtle drawing of a cake and a clickable Tkinter birthday card. Every program includes its real output or a screenshot, so you can see what you will get before you run it.
All programs were tested with Python 3.12.5 on Windows 11, pyfiglet 1.0.4, colorama 0.4.6, Tk 8.6. The Turtle and Tkinter screenshots are from the actual windows. For the full reference, see the turtle module documentation.
Quick answer: the simplest happy birthday code
name = "Emma"
print(f"Happy Birthday, {name}!")
print("Wishing you a year full of joy and bug-free code.")
Output:
Happy Birthday, Emma!
Wishing you a year full of joy and bug-free code.
Change name and run the file with python birthday.py. The f before the string lets you put the variable straight inside the text.
1. Happy birthday song with a for loop
The birthday song has four lines, and only the third one is different. A for loop with one if prints the whole song without repeating yourself:
name = "Emma"
for line in range(1, 5):
if line == 3:
print(f"Happy birthday, dear {name},")
else:
print("Happy birthday to you,")
Output:
Happy birthday to you,
Happy birthday to you,
Happy birthday, dear Emma,
Happy birthday to you,
2. Big ASCII-art banner with pyfiglet
For a banner that fills the terminal, use the pyfiglet package. It is not part of Python, so install it first:
pip install pyfiglet
import pyfiglet
banner = pyfiglet.figlet_format("Happy Birthday", font="slant")
print(banner)
Output:
__ __ ____ _ __ __ __
/ / / /___ _____ ____ __ __ / __ )(_)____/ /_/ /_ ____/ /___ ___ __
/ /_/ / __ `/ __ \/ __ \/ / / / / __ / / ___/ __/ __ \/ __ / __ `/ / / /
/ __ / /_/ / /_/ / /_/ / /_/ / / /_/ / / / / /_/ / / / /_/ / /_/ / /_/ /
/_/ /_/\__,_/ .___/ .___/\__, / /_____/_/_/ \__/_/ /_/\__,_/\__,_/\__, /
/_/ /_/ /____/ /____/
pyfiglet has hundreds of fonts. Try "big", "standard", "banner3" or "doom", and print the full list with pyfiglet.FigletFont.getFonts(). Here is the birthday person’s name in the big font:
import pyfiglet
name = "Emma"
print(pyfiglet.figlet_format(name, font="big"))
print(f"Have a wonderful birthday, {name}!")
Output:
______
| ____|
| |__ _ __ ___ _ __ ___ __ _
| __| | '_ ` _ \| '_ ` _ \ / _` |
| |____| | | | | | | | | | | (_| |
|______|_| |_| |_|_| |_| |_|\__,_|
Have a wonderful birthday, Emma!
3. Birthday cake made of text
No packages needed: string multiplication and center() draw a cake with one candle for every year (so keep age small, or cap it). The cake grows to fit the name:
name = "Emma"
age = 7
width = max(age * 2 + 5, len(name) + 8, 20) # inside width of the cake
flames = " ".join("*" * age)
candles = " ".join("|" * age)
print(" " + flames.center(width))
print(" " + candles.center(width))
print("." + "-" * width + ".")
print("|" + "~" * width + "|")
print("|" + "Happy Birthday".center(width) + "|")
print("|" + f"{name}!".center(width) + "|")
print("|" + "~" * width + "|")
print("'" + "-" * width + "'")
Output:
* * * * * * *
| | | | | | |
.--------------------.
|~~~~~~~~~~~~~~~~~~~~|
| Happy Birthday |
| Emma! |
|~~~~~~~~~~~~~~~~~~~~|
'--------------------'
4. Ask for the name and age with input()
To make the program personal, ask for the details when it runs. The small ordinal() function turns 21 into “21st” and 12 into “12th”, which a plain f"{age}th" gets wrong:
def ordinal(n):
if 10 <= n % 100 <= 20:
suffix = "th"
else:
suffix = {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")
return f"{n}{suffix}"
name = input("Whose birthday is it? ").strip().title()
age = int(input("How old are they turning? "))
print()
print(f"Happy {ordinal(age)} birthday, {name}!")
print(f"{age} years of awesome. Enjoy your day!")
Output (the typed values are emma and 21):
Whose birthday is it? emma
How old are they turning? 21
Happy 21st birthday, Emma!
21 years of awesome. Enjoy your day!
.strip().title() cleans up the name, so ” emma” becomes “Emma”. If the user types something that is not a number for the age, int() raises a ValueError; wrap it in try/except if you share the program with others.
5. Colourful birthday message with colorama
ANSI colour codes make the terminal look like a party. The colorama package gives them readable names and makes them work in the old Windows console too:
pip install colorama
from colorama import Fore, Style, just_fix_windows_console
just_fix_windows_console() # makes colours work in the old Windows console
name = "Emma"
print(Fore.MAGENTA + Style.BRIGHT + "*" * 30)
print(Fore.YELLOW + Style.BRIGHT + f"Happy Birthday, {name}!".center(30))
print(Fore.MAGENTA + Style.BRIGHT + "*" * 30)
print(Fore.CYAN + "Wishing you cake, fun and zero bugs." + Style.RESET_ALL)
Style.BRIGHT makes the colours bold, and Style.RESET_ALL at the end switches your terminal back to its normal colours.
6. Animated birthday message in the terminal
Printing one character at a time and pausing with time.sleep() gives a typewriter effect. For the blinking line, \r moves the cursor back to the start of the line, so the text is printed and erased in the same place instead of scrolling:
import sys
import time
def type_out(text, delay=0.08):
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(delay)
print()
def blink(text, times=4, delay=0.4):
for _ in range(times):
sys.stdout.write("\r" + text)
sys.stdout.flush()
time.sleep(delay)
sys.stdout.write("\r" + " " * len(text))
sys.stdout.flush()
time.sleep(delay)
print("\r" + text)
type_out("Happy Birthday, Emma!")
blink("*** Have a wonderful day! ***")
Final output:
Happy Birthday, Emma!
*** Have a wonderful day! ***
Call sys.stdout.flush() after each write, otherwise Python may hold the characters in a buffer and print the whole line at once. Run it in a real terminal; some IDE consoles ignore \r.
7. Draw a birthday cake with Python Turtle
Turtle comes with Python and opens a drawing window. This program draws a two-layer cake with five candles and writes the message above it. One rectangle() helper does most of the work. (More on colours and fonts in Python Turtle colors and Python Turtle font.)
import turtle
screen = turtle.Screen()
screen.setup(640, 520)
screen.bgcolor("#fff4e6")
screen.title("Happy Birthday!")
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
def rectangle(x, y, width, height, color):
pen.penup()
pen.goto(x, y)
pen.setheading(0)
pen.pendown()
pen.color(color)
pen.begin_fill()
for side in (width, height, width, height):
pen.forward(side)
pen.left(90)
pen.end_fill()
# plate, two cake layers and white icing
rectangle(-200, -170, 400, 12, "#9e9e9e")
rectangle(-170, -158, 340, 110, "#f48fb1")
rectangle(-170, -66, 340, 18, "white")
rectangle(-120, -48, 240, 90, "#ce93d8")
rectangle(-120, 26, 240, 16, "white")
# five candles with flames
for x in range(-90, 91, 45):
rectangle(x - 6, 42, 12, 50, "#64b5f6")
pen.penup()
pen.goto(x, 104)
pen.dot(20, "#ffb300")
pen.dot(9, "#fff176")
pen.goto(0, 150)
pen.color("#c2185b")
pen.write("Happy Birthday, Emma!", align="center", font=("Arial", 28, "bold"))
turtle.done()
pen.speed(0) draws at full speed; use pen.speed(3) if you want to watch the cake appear. turtle.done() keeps the window open until you close it.
8. Clickable birthday card with Tkinter
Tkinter is also built into Python. This card shows a greeting and a button; clicking the button reveals the message inside the card:
import tkinter as tk
def open_card():
message.config(text="Wishing you a year full of joy,\ngood health and clean code!")
button.config(state="disabled")
root = tk.Tk()
root.title("Birthday Card")
root.configure(bg="#fce4ec")
title = tk.Label(root, text="Happy Birthday, Emma!", font=("Georgia", 22, "bold"),
fg="#ad1457", bg="#fce4ec")
title.pack(padx=40, pady=(30, 10))
message = tk.Label(root, text="Click the button to open your card.",
font=("Arial", 12), bg="#fce4ec")
message.pack()
button = tk.Button(root, text="Open card", font=("Arial", 12, "bold"),
bg="#ad1457", fg="white", padx=12, command=open_card)
button.pack(pady=20)
root.mainloop()
command=open_card connects the button to the function, and config() changes the label text while the window is open. To let the user type the name into the window instead, add an Entry widget.
9. Countdown to the next birthday with datetime
A birthday program is more useful when it knows the date. This one tells you how many days are left and how old the person will be, and it handles people born on 29 February (their birthday falls on 28 February in non-leap years):
from datetime import date
def next_birthday(born, today):
for year in (today.year, today.year + 1):
try:
day = born.replace(year=year)
except ValueError: # born on 29 February, not a leap year
day = date(year, 2, 28)
if day >= today:
return day
def countdown(born, today):
bday = next_birthday(born, today)
days = (bday - today).days
age = bday.year - born.year
if days == 0:
return f"Today is the day. Happy birthday, you are {age}!"
return f"{days} days until you turn {age} on {bday:%A %d %B %Y}."
today = date(2026, 9, 21) # use date.today() in your own code
print(countdown(date(1996, 12, 25), today))
print(countdown(date(2000, 2, 29), today))
print(countdown(date(1990, 9, 21), today))
Output:
95 days until you turn 30 on Friday 25 December 2026.
160 days until you turn 27 on Sunday 28 February 2027.
Today is the day. Happy birthday, you are 36!
The date is fixed here so you get the same output as above. In your own code, use today = date.today().
Which birthday program should you use?
| Program | Needs install? | Best for |
|---|---|---|
| print() / for loop | No | Beginners, a quick message |
| pyfiglet banner | pip install pyfiglet | A big message that fills the terminal |
| Text cake | No | Practising strings and loops |
| input() version | No | Personal wishes for anyone |
| colorama | pip install colorama | Colourful terminal output |
| Animation | No | A typewriter and blinking effect |
| Turtle cake | No (built in) | A picture you can screenshot and send |
| Tkinter card | No (built in) | A small desktop app with a button |
| datetime countdown | No | Days until the next birthday |
Troubleshooting
ModuleNotFoundError: No module named 'pyfiglet': install it withpip install pyfiglet(orpython -m pip install pyfigletso it goes into the same Python you run the file with).- The Turtle window opens and closes at once: add
turtle.done()as the last line. turtle.Terminatorerror when you run the Turtle program twice: this happens in some editors after closing the window; restart the Python shell or run the file from a terminal.- You see codes like
[35minstead of colours: calljust_fix_windows_console()as shown, or use Windows Terminal. - Don’t name your file
turtle.pyorpyfiglet.py: Python would import your file instead of the real module.
Frequently asked questions
How do I write happy birthday code in Python?
Set a variable with the name and print it with an f-string: print(f"Happy Birthday, {name}!"). For something more visual, use pyfiglet for an ASCII-art banner or Turtle to draw a birthday cake.
How do I make a happy birthday animation in Python?
In the terminal, print one character at a time with sys.stdout.write(), sys.stdout.flush() and time.sleep(), and use \r to make a line blink. For a graphical animation, draw with Turtle at a lower speed() so you can watch the drawing appear.
How do I print Happy Birthday in big letters in Python?
Install pyfiglet with pip install pyfiglet and print pyfiglet.figlet_format("Happy Birthday", font="slant"). Change the font name to get a different style.
Do I need to install anything for these birthday programs?
Only pyfiglet and colorama need pip install. print(), Turtle, Tkinter and datetime all come with Python.
More fun Python projects to try:
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn