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
How to Generate a Random Password Using Ruby? Generating a random password is a fundamental task in cybersecurity and application development. Creating a secure random password is very easy with the Ruby library. This article will show how to generate a random password in Ruby in Python. Generate a Random Password Using RubyBelow are the code e
2 min read
Find jokes on a user provided topics using Python Prerequisites : Http Request methods â Python requestspyfiglet Python requests module helps us make Http requests to specified URL. In this article we will make a json request to this url: Â https://icanhazdadjoke.com/ and develop a project wherein the user enters any word and the output would be a j
2 min read
Pytest Tutorial - Unit Testing in Python using Pytest Framework In the process of development of applications, testing plays a crucial role. It ensures the developers that the application is error-free and the application is running as per the expectation. It should be done to ensure a seamless experience for the user. In this article, we will learn about testin
5 min read
Take Screenshots at Random Intervals with Python In this article, we will learn how to take screenshots at Random Intervals with Python. Method 1: Using pyautogui.screenshot() pyautogui: The pyautogui library provides the screenshot() method, The screenshot() method is used to take a screenshot. We will use the random library and sleep() method to
5 min read