Python Script to create random jokes using pyjokes
Last Updated :
02 Dec, 2020
Python supports creation of random jokes using one of its libraries. Let us explore it a little more, Pyjokes is a python library that is used to create one-line jokes for programmers. Informally, it can also be referred as a fun python library which is pretty simple to use. Let us see how you can actually use it to perform the required task,
Installation
You can simply install it using pip with the following command:
pip install pyjokes
Usage
Now to use it, we need to import the installed library in our python Script using the following command:
import pyjokes
Before, moving further towards our python script, it is necessary to get familiar with the two functions of Pyjokes library, namely get_joke() and get_jokes().
Functions
Syntax:
get_joke(language,category)
As the name suggests, this function is used to actually return a single joke from a certain category and in a particular language, (Categories and languages will be introduced later in this article).
Syntax:
get_jokes(language,category)
It is similar to the get_joke() function, the only difference lies in the fact that instead of returning a single joke, it returns a list of random jokes from a certain category and in a particular language.
Parameters
Language and category are the two parameters of get_joke() and get_jokes() functions.
Language specifies in which language you want the joke(s) to be displayed. By default, it is set to "en" that returns jokes in English. All other possible values for language parameter are described below:
Language | Values |
en | English |
de | German |
es | Spanish |
it | Italian |
gl | Galician |
eu | Basque |
Similarly, the category parameter specifies the category in which you want joke(s) to be displayed. By default, it is set to "neutral". All other possible values for the category parameter are described below:
Category | Values |
neutral | Neutral geeky jokes |
twister | Tongue-twister |
all | All types of joke |
Below are some examples to understand better:
Example 1: Using get_joke() to generate a single joke
Python3
# importing installed library
import pyjokes
# using get_joke() to generate a single joke
#language is english
#category is neutral
My_joke = pyjokes.get_joke(language="en", category="neutral")
print(My_joke)
Output:
Example 2: Using get_jokes() to generate a list of jokes
Python3
import pyjokes
# using get_jokes() to generate a whole list of jokes
# language is german
# category is twister
list_of_jokes = pyjokes.get_jokes(language="de", category="twister")
# traversing through the generated list of jokes
# Range of i may change, depending on the number of jokes
# you want to display
for i in range(0, 4):
print(list_of_jokes[i], sep='\n')
Output:
Similar Reads
How to generate a random phone number using Python? In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6. Approach: We will use the random library to generate random numbers.The number should contain 10 digits.The first digit should start with
1 min read
Create a Random Password Generator using Python In this article, we will see how to create a random password generator using Python. Passwords are a means by which a user proves that they are authorized to use a device. It is important that passwords must be long and complex. It should contain at least more than ten characters with a combination
5 min read
How to build a Random Story Generator using Python? In this section, we are going to make a very interesting beginner-level project of Python. It is a random story generator. The random story generator project aims to generate random stories every time user executes the code. A story is made up of a collection of sentences. We will choose random phra
4 min read
Python | Random Password Generator using Tkinter With growing technology, everything has relied on data, and securing this data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. An easy password can be hacked easily and all personal information can be misused. In order to prevent such things and keep th
4 min read
Secrets | Python module to Generate secure random numbers The secrets module is used for generating random numbers for managing important data such as passwords, account authentication, security tokens, and related secrets, that are cryptographically strong. This module is responsible for providing access to the most secure source of randomness. This modul
3 min read
Dice Rolling Simulator using Python-random In this article, we will create a classic rolling dice simulator with the help of basic Python knowledge. Here we will be using the random module since we randomize the dice simulator for random outputs. Function used: 1) random.randint(): This function generates a random number in the given range.
2 min read