Building a Mad Libs Game | Python

thecodingpie . . 8 min read

Howdy Folks, In this tutorial you are going to build a simple mad libs game using python. If you are a python beginner, then this must be the best place to start.

You can get the complete code here from my Github repo - Mad-Libs-Python

Now, without further ado, let's get started... Hey wait, but what is Mad Libs Game?

Mad Libs

According to Wikipedia, "Mad Libs is a phrasal template word game where one player prompts others for a list of words to substitute for blanks in a story before reading aloud."

If you still didn't get it, let me tell you an example. Imagine you had a story like this:

mad libs unfilled

See, the story has many key words replaced with blanks. Beneath each blank is specified a category, such as exclamation, noun, a verb like that. Now you ask another player to contribute a word of the specified type for each blank, but without revealing the context for that word. Finally, after filling all the blanks with the parts of speech given by that another player, the completed story is read aloud. The result is usually comic, surreal, and somewhat nonsensical 😆. For example, the above story after completion might look like this:

filled mad libs funny

See, it's that simple. That's exactly what we are going to do but using python. It's going to be a lot of fun...

The breakdown

As I said, we need a story left with blank spaces. For the sake of simplicity, I am going to use the exact story we saw above which I got from Wikipedia.

the story we are going to use

Now we have the story, the next thing is to get some parts of speech (like an adverb, noun, etc) from the user, right? So we prompt the user to enter an exclamatory word, an adverb, a noun, and an adjective.

When they do so, we will store them in their corresponding variables.

Now we have the words, the next thing to do is to reveal the entire story by including the substitute words.

That's it, that's all we are going to do.

What you will learn?

By doing this simple project, you will learn the following:

  • How to run a python file in the Terminal
  • How to work with string data type in python
  • The concepts of variables
  • How to take input from the user
  • And how to output something to the screen

Things you will need

  • The very first thing you will need is the python itself. I am going to use python3. You can read this post to learn how to setup python3 on any operating system -
  • After setting up python3, you will need a modern code editor like visual studio code. Yes, of course, you can do this simple project inside the default python IDLE or things like that, but the benefit of using a modern code editor like visual studio code is that it comes pre-baked with all the things you will ever need during your programming journey. It has a pre-built terminal, Integrated Git, debugging tools, extensions, and much much more. And more than anything else, it is completely free and open-source.

You can download visual studio code from here according to your Operating system -

With these things set up, now you are good to go.

Getting Started

  • Create a folder named Mad Libs anywhere on your computer.
  • Now just open visual studio code.
  • Then inside visual studio code, click on File > Open Folder and choose the folder you created Mad Libs.
  • Now click on this icon to create a new file and name it as

new file creation icon

  • Now you will have something similar to this:

file opened

That's it now you are good to write the python code.

The Prerequisites

In this section, we are not going to make the game, instead, I am trying to teach you how to take input, store them in variables, and things like that. If you already know that, then please skip to The Actual Game section or else just continue.

Hmm... What are we going to write in this file? Yes, absolutely some python codes, but what are the things we need to do in order to make a game like mad libs?

  • The very first thing we need to do is to prompt the user to type something and take the input from the user, right? We can do that using input() function like this. You can start type along with me:
input("What is your name? ")

This function will throw whatever message you put inside those () brackets onto the screen and takes whatever the user types as input until they hit the Enter key.

Wait we are not going to run it now. Before that, we need to store the value the user had entered somewhere right?

Here's where the concept of variable comes into play. In a more abstract manner, you can think of a variable as a box with a unique name:

variable imagined as a box

I got that image from this site -

All variables are made up of three parts: a name, its data type, and a value. But in python, you don't have to specify the data types externally as you would do in other languages. Python is smart enough to figure it out for you 😇.

So let's store the value the user had entered in a variable called name and try to print() it back to the screen.

Edit your code to look like this:

name = input("What is your name? ")

To output something to the screen, we use the print() function. I hope you got that.

Now let's try to run this python program in the terminal:

  • From within visual studio code, click on the Terminal > New Terminal:

opening terminal inside visual studio code

Now a terminal will pop up. In the terminal, enter the following and hit the Enter key:


I assume you named your file as I did, else put whatever the name of your file instead of and run it. Now you would see whatever name you have given as the input like this:

sample output

Now you are efficient to make the actual game.

The Actual Game

Ok, first of all just clear whatever you typed up to this point inside the file and make it empty.

Now let's create the Madlibs game.

In our story, we need 4 parts of speech / in our case 4 variables namely:

  • exclamation
  • adverb
  • noun
  • And an adjective

So we are going to prompt the user to input them and store them in the corresponding variables. Type the following code into the file:

# taking the input from the user
exclamation = input("Enter an exclamation word and hit enter: ")
adverb = input("Enter an adverb and hit enter: ")
noun = input("Enter a noun and hit enter: ")
adjective = input("Enter an adjective and hit enter: ")

Now we have the needed words. The next step is to concatenate them into our story.

There are so many ways we can do that. But the most efficient way is to use what's called an f-string. It was introduced in python 3.6.

Let's create a variable called story to store our story with the above variables included. Type this below the above code:

# our story
story = f"{exclamation}! he said {adverb} as he jumped into his convertible {noun} and drove off with his {adjective} wife."

See how easy it is to write string concatenation using f-string! Just put an f before the string and use {} to reserve spaces. Then inside the {}, we can put whatever variable we need and its value will get replaced. And it's more readable too.

Finally, let's reveal the entire story by using print():

# printing our story

That's it, you have made your first python game, the great Mad Libs!

Let's run it:

  • Open the Terminal inside visual studio code if you hadn't opened it yet.
  • Then type the following code and hit the Enter key:

This is my output which I got:

final output

See its funny 😆!

Complete Code

Here is the final code for the file. Make sure the code you typed up to this point inside the is exactly like the following:

# taking the input from the user
exclamation = input("Enter an exclamation word and hit enter: ")
adverb = input("Enter an adverb and hit enter: ")
noun = input("Enter a noun and hit enter: ")
adjective = input("Enter an adjective and hit enter: ")

# our story
story = f"{exclamation}! he said {adverb} as he jumped into his convertible {noun} and drove off with his {adjective} wife."

# printing our story

Wrapping Up

I hope you enjoyed this tutorial.

As an exercise, why don't you try creating complex mad libs with more than 4 inputs? You can get some good mad libs story from the internet.

If you had any doubts, then please comment them below. Thank you ;)

About Me

Hey folks, my name is Aravind, and I am the man behind this website. To know more about me, check out the About Me page. If you like and enjoy my content, then please consider supporting what I do through - Buy Me a coffee.