Javatpoint Logo
Javatpoint Logo

Create a Quote Generator in Python

Task: User enters a hash tag and based on that tag, the Python program must be able to generate quotes one after the other. If the program isn't able to find any quote with the given tag, it has to print "Quote with given hash tag isn't found".


We need to use a data set for this task. The data set must contain quotes and tags that can be associated with each quote. The program can then search the data set for the user specified tag and if it finds any quote, it can print the quote. We can find a lot of data sets already in the internet uploaded by developers across various websites like github, kaggle, etc.

In this tutorial, we are using a dataset from kaggle. Here is how, you can find it:

  1. Go to Google search and search for "Quotes dataset".
  2. You can find different sites providing different datasets:
    Create a Quote Generator in Python
  3. We've chosen the first dataset from Kaggle:
    Create a Quote Generator in Python
  4. Kaggle is an online community of developers, Machine learning enthusiasts and data scientists collaborating on projects and sharing their works. As you can observe, the dataset Quotes is contributed by one of the developers from the community which we can easily download and use for our projects.
  5. Click on Download and wait for it to download, it is a large file so it might take some time.
    Create a Quote Generator in Python
  6. On scrolling down, you can find the details of the dataset in kaggle. Now, open the downloaded file.
  7. It is a .json file. Open the .json file using text document

A JSON file:

JSON stands for "JavaScript Object Notation". It is a language-independent standard data format. It is used in various applications of data especially for data interchange between web applications and servers. It was derived from JavaScript and hence the name.It uses human-readable text to store and transmit data. The data is arranged in the form of attribute-value pairs. A .json file can be imported into excel for a structured tale format.

Here are two quotes from the quotes dataset:

Now, if we try to understand the underlying table structure of the file:

Quote Author Tags Popularity Category
Don't cry because it's over, smile because it happened Dr. Seuss attributed-no-source, cry, crying, experience, happiness, joy, life, misattributed-dr-seuss, optimism, sadness, smile, smiling 0.15566615566615566 life
I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best. Marilyn Monroe attributed-no-source, best, life, love, mistakes, out-of-control, truth, worst 0.12912212912212911 life
  • Make sure that the python file you are coding in and the json file with the dataset are in the same folder.
  • If not provide the complete path of the json file in the code.

Now, we'll write a program to read the json file, filter it with quotes with the user specified tag, sort it according to maximum popularity and then print the most popular quotes one after the other:



Enter a tag:happiness
Popularity:  0.15566615566615566
Quote:  Don't cry because it's over, smile because it happened.


Popularity:  0.025575025575025574
Quote:  Love is that condition in which the happiness of another person is essential to your own.


Popularity:  0.01855001855001855
Quote:  Time you enjoy wasting is not wasted time.


Popularity:  0.01366001366001366
Quote:  It's so hard to forget pain, but it's even harder to remember sweetness. We have no scar to show for happiness. We learn so little from peace.



  1. The first line imports the json module, which is used to load the JSON data from the file.
  2. The with statement opens the file quotes.json in read mode and assigns it to the variable file. The encoding parameter is set to "utf-8" to ensure that the file is read correctly if it contains non-ASCII characters.
  3. The json.load() function is used to parse the JSON data from the file and convert it into a Python object.
  4. The input() function is used to prompt the user to enter a tag.
  5. A dictionary called dictionary is created to store the quotes that have the tag entered by the user, along with their popularity scores.
  6. A loop iterates over each quote in the data object. If the quote has the tag entered by the user, the popularity score and quote text are added to the dictionary object.
  7. If the dictionary object is empty (i.e., no quotes were found with the specified tag), a message is displayed to inform the user.
  8. If the dictionary object is not empty, the popularity scores are sorted in descending order and the sorted quotes are displayed to the user one by one.
  9. After each quote is displayed, the user is prompted to enter "Y" to display the next quote or "N" to exit the program. If the user enters "Y", the loop continues to the next quote. If the user enters "N", the loop is broken and the program exits.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA