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
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:
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:
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.
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.
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.
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
- Change
"Sam"to a real name in any example. - Join two programs: type out the message, then print the cake.
- Add a second verse to the song, or count down 3, 2, 1 with
time.sleep(1)before it starts. - Draw a heart for them too, with the Python heart code.
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 courseMore fun python code
How to write "I love you" in Python
Print "I love you" in Python, repeat it 100 times, and turn it into 143, binary and Morse code. Every example runs in your browser.
Python heart code: draw a heart with turtle or text
Copy-and-run Python heart code: a heart made of stars, a heart filled with LOVE, and red turtle hearts. Change the colours and add a name.
More free ways to learn
Practice
Free Python practice online: exercises with answers you check in your browser, plus 79 coding games, one for every beginner topic.
Teach
Free resources for teaching Python to kids and beginners: a Scratch to Python guide, printable worksheets with answers, and a game-based course.
Guides
Short guides on learning Python with games, how long it takes, and where to start.