Explore

Close
Python Programs Share

Create Joke Generator In Python With Code

Create Joke Generator In Python

Looking for a way to build a python joke generator then you are at the right place today in this python tutorial l will show you how to create a joke generator in python programming with code so read till the end.

Laughter is the best medicine so to laugh you need jokes, so I have decided to create this python jokes program which will generate random jokes in python.

To do this we will use the pyjokes python library which will provide us with jokes. I will show you everything you require and provide you with the python joke generator code, so read till the end.

Python Joke Generator Code


# Python joke generator program
import pyjokes

joke = pyjokes.get_joke(language="en", category="all")

print(joke)

Above is the python code to generate jokes in python. First create a new folder on your computer, open it in a code editor of your choice, I use VS Code. Now create a python file and copy and paste the above python code.

As I have said we used the pyjokes python library for this project so you need to install this library. To install, paste the below command in your terminal.


pip install pyjokes

It will install pyjokes in your project. Now you are ready to run this program. To run it, open a command prompt at the project folder location and paste the below command.


python filename.py

The above command will run this program. It will print a joke in the command prompt every time you run this program, it will print a new joke.

Below is the output of some jokes printed by this joke generator program. See if you can get them.


>> To understand recursion first you must understand recursion

As you can see, we successfully created a python joke generator program with just 3 lines of code. If you want to run this program right now, you can use this online python compiler to test.

Code Explanation

Now let’s see how the three lines of code works in our joke program. First we import the pyjokes library and all the work is done by the library.


joke = pyjokes.get_joke(language="en", category="all")

Now we just have to use one function this library provides which is the get_joke() function as you can see above.

It has 2 parameters: language and category. In the language, you have to enter the language code you want. I wanted english so I entered en.


pyjokes.get_joke(language="en", category="all")
pyjokes.get_joke(language="en", category="neutral")
pyjokes.get_joke(language="en", category="chuck")

In the category we have 3 parameters, all, neutral and chuck I have chosen all you can choose anything. You can find out about it more here: Pyjokes usage.

Summary

This was the tutorial on creating a joke generator program in python. I hope you found this tutorial helpful and useful. Do share this with your friends who might be interested in this program.

Here are some more python tutorials you will find interesting:

I hope you found what you were looking for from this tutorial and if you want more python tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂