Project: Mad-Libs Story Generator
Time to build your first project. A Mad-Libs game asks for a few words (a name, a place, an animal) and drops them into a story to create something funny. You will build one using variables and a small piece of new magic: the f-string.
What You'll Build
A short program that stores some words in variables and prints a complete story with those words slotted in. Change the words, run it again, and you get a brand new story.
- Word variablesname, place, animal
- Build the storyf-string
- Print itrun and read
The New Tool: f-Strings
An f-string is a string with an f in front. Anything inside curly braces is replaced with the value of that variable. It is the cleanest way to build text from variables:
Compare that to gluing pieces with + and str(). The f-string is shorter and much easier to read. You will use f-strings constantly from here on.
Step 1: Store the Words
Step 2: Build and Print the Story
Now slot the words into a sentence with an f-string:
Build It Yourself
Now put the whole thing together. Use the four variables and one f-string to produce the exact story below.
Make It Your Own
Change the words to whatever you like, and try adding another blank to the story (maybe an adjective or a second place). The playground below is yours to experiment in.
Challenge Ideas
- Add a
foodvariable and mention it in the story - Print a second line that reacts to the story, like a headline
- Make the number part of the sentence do math, for example
{number * 2}legs
Key Takeaways
- f-strings slot variable values into text:
f'Hi, {name}' - Store each blank in its own clearly named variable
- Building small programs from a few variables is real programming
- Change the inputs, rerun, and watch the output change

