Happy birthday code in Python

Want to wish someone happy birthday with code? Here are five short Python programs, from a cake made of text to a countdown of the days until your next birthday. Press Run to try each one, then put in a real name.

On this page
  1. A birthday cake made of text
  2. Sing the song with a loop
  3. Make the message type itself out
  4. A cake with the right number of candles
  5. How many days until your birthday?
  6. Make it your own

A birthday cake made of text

Each print() draws one row of the cake. The candles line up because each row has the same number of spaces in front:

main.py
Output
        i  i  i  i  i
        |  |  |  |  |
      __|__|__|__|__|__
     |~~~~~~~~~~~~~~~~~|
     |  HAPPY BIRTHDAY |
     |~~~~~~~~~~~~~~~~~|
     |_________________|

Sing the song with a loop

The song has four lines, and only the third one is different. A loop prints all four, and an if picks the special line:

main.py
Output
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Sam!
Happy birthday to you!

Make the message type itself out

This prints one letter at a time, with a short pause in between, so the message appears as if someone is typing it. end="" keeps the letters on the same line, and flush=True shows each one straight away.

main.py
Output
Happy birthday, Sam! Have the best day!

A cake with the right number of candles

Ask for a name and an age, and the cake grows to fit. "i" * candles repeats the candle, and center() puts the message in the middle of the cake. The cake stops at 15 candles, so it still fits on the screen.

main.py
Output
Whose birthday is it? Sam
How old are they today? 9
    i i i i i i i i i
    | | | | | | | | |
  _____________________
 | Happy birthday Sam! |
 |~~~~~~~~~~~~~~~~~~~~~|
 |_____________________|
Sam is 9 today, and 10 next year!

Shown with the answers 'Sam', '9'. Press Run to type your own.

How many days until your birthday?

Python's datetime module knows today's date and can count the days between two dates. If your birthday has already happened this year, the program looks at next year's instead.

main.py
Output
Press Run, and type your answers when it asks.

Press Run and type your own birthday. The answer changes every day, so it's worked out fresh each time you run it.

Make it your own

Play the game version

Each of these games teaches one idea from this page, with a short narrated video and six levels to clear.

Questions people ask

What is the code for happy birthday in Python?

The simplest is print("Happy birthday!"). To include a name, use an f-string: name = "Sam" then print(f"Happy birthday, {name}!").

How do I make the text appear one letter at a time?

Loop over the message, print each letter with print(letter, end="", flush=True), and pause with time.sleep(0.08) after each one.

How do I print the happy birthday song in Python?

Use for line in range(1, 5): and print "Happy birthday to you!" each time, except when line == 3, where you print "Happy birthday, dear" and the name.

How do I count the days until a birthday in Python?

Import date from datetime, make the next birthday with date(year, month, day), and subtract today: (birthday - date.today()).days.

Learn Python with games

Every idea on this page has its own game in the course, with a short video and six levels. The three games in Module 1 are free: no account, nothing to install.

Every idea on this page has its own game in the course. Pick up where you left off.

Play the first game freeContinue the course

More fun python code