Python quote module – Beginners Introduction

Filed Under: Python Modules
Python Quote Module

Hey, readers! In this article, we will be focusing on Python quote module, in detail.

So, let us begin!! 馃檪

Also read: How to generate random numbers in Python?


Python quote module – Crisp overview

Python offers us with quote module that enables us to output “quotes”. Imagine you have a mobile application created for mental health happiness and you need the users to receive a quote every morning. In that scenario, what can be better than having them randomly generated using this module? 馃檪

With quote module, we can perform the below functions–

  1. Generate random keywords (string)
  2. Generate random quotes
  3. Generate random quotes from random generated keywords.

Let us have a look at them in the below section.


Step 1: Obtain random keywords

The quote module enables us to have random keywords generated easily in the form of quotes in the background using the RandomWords module. For the same, will need to make use of the random_words library of Python. With this module, we can generate a random work using the get_random_word() function as shown–

Example–

Here, we have first imported the RandomWords module. Further, we generate an object and refer it further for object creation. Then, we make use of get_random_word() function to get a word from the English dictionary generated at random.

from random_word import RandomWords
rm = RandomWords()
word = rm.get_random_word()
print(word)

Output–

thunder-tube

Feature 2: Generation of a random quote

Having generated a random word now is the time for us to generate a random quote. We’ll make use of the in-built functions of the quote module to generate a quote at runtime.

We can also customize the number of quotes to be generated with the function as shown below–

Example–

In this example, we have imported the quote module and have made use of quote() function to generate a quote using the keyword provided as parameter. Also, as we have set the limit to 2, it generates 2 quotes at maximum.

from quote import quote
op = quote('family',limit=2)
print(op)

Output–

[{'author': 'J.R.R. Tolkien', 'book': 'The Fellowship of the Ring', 'quote': "I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve."}]

Feature 3: Generate a random quote using some randomly generated keyword

In the above scenarios, we have generated quotes using a keyword as a parameter. But what if we do not want quotes around a single parameter, that is, we want quotes to be out of keywords generated at random?

In such scenario, we have to do the following–

  1. At first, import the RandomWords module.
  2. Generate random word using get_random_word() function.
  3. Import the quote module.
  4. To the quote() function, pass the randomly generated keyword (in Step 2) as a parameter.
  5. View the result.

Example–

Here, first we have generated a random keyword using the get_random_word() function. Further, we have passed that word to the quote() function and have generated 1 quote at max.

from random_word import RandomWords
from quote import quote
 
rm = RandomWords()
wrd = rm.get_random_word()
print("Keyword: ",wrd)
 
result = quote(wrd, limit=1)
for i in range(len(result)):
    print("Quote: ",result[i]['quote'])

Output–

Keyword:  trendle
Quote:  a western hero, as writer J. Bryan III described it in a 1939 Saturday Evening Post article, 鈥渨ho goes around righting wrongs against tremendous odds and then disappearing immediately.

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content