{"id":16288,"date":"2021-05-19T16:24:14","date_gmt":"2021-05-19T16:24:14","guid":{"rendered":"https:\/\/www.askpython.com\/?p=16288"},"modified":"2023-02-16T19:56:52","modified_gmt":"2023-02-16T19:56:52","slug":"quote-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/quote-module","title":{"rendered":"Python quote module: How to generate random quotes?"},"content":{"rendered":"\n<p>Hello there today let&#8217;s learn how to get quotes from various authors using the <strong>quote<\/strong> module in python. So let&#8217;s begin!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Generating a random keyword<\/h2>\n\n\n\n<p>To get quotes from various backgrounds we will generate a random keyword every time and the program will return a quote from a particular author revolving around the keyword.<\/p>\n\n\n\n<p>To get any random English word we make use of the <code>random_word<\/code> module. The random_word module can be used to generate either a single random word or a list of random words.<\/p>\n\n\n\n<p>You can install the module using the <code>pip<\/code> command if importing the same gives you an error. Let&#8217;s look at the following lines of code first.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfrom random_word import RandomWords\nr = RandomWords()\nw = r.get_random_word()\nprint(w)\n<\/pre><\/div>\n\n\n<p>Here we imported a function named <code>RandomWords<\/code> from the module and created an object of the same which will be helpful to extract words.<\/p>\n\n\n\n<p>Later we applied the <code>get_random_word<\/code> function on the object created to create a random word and stored the same into a variable.<\/p>\n\n\n\n<p>The code generated a random word from English dictionary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get a random quote using the quote module in Python<\/h2>\n\n\n\n<p>Now that we have a random keyword with us, the next step is to generate a quote for the keyword using the <code>quote<\/code> library.<\/p>\n\n\n\n<p>If importing the library gives an error make sure you install quote library using the <code>pip<\/code> command beforehand.<\/p>\n\n\n\n<p>Let&#8217;s look at the following code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfrom quote import quote\nres = quote(&#039;family&#039;,limit=1)\nprint(res)\n<\/pre><\/div>\n\n\n<p>To generate a random quote we will be using the <code>quote<\/code> function from the quote module. The quote function requires a keyword to search for the quotes.<\/p>\n\n\n\n<p>We also set the limit value to limit the no of quotes being generated. But on printing the output we get something like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;{&#039;author&#039;: &#039;J.R.R. Tolkien&#039;, &#039;book&#039;: &#039;The Fellowship of the Ring&#039;, &#039;quote&#039;: &quot;I don&#039;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.&quot;}]\n<\/pre><\/div>\n\n\n<p>The reason behind the same is that the quote function returns a list of dictionaries where each <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" class=\"rank-math-link\">dictionary<\/a> contains information about a particular quote.<\/p>\n\n\n\n<p>So we will be extracting the quote value from the dictionary. To do the same we will use the following lines of code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfor i in range(len(res)):\n    print(res&#x5B;i]&#x5B;&#039;quote&#039;])\n<\/pre><\/div>\n\n\n<p>What we are doing here is traversing through the list and for each dictionary value we will print only the value next to the <code>quote<\/code> key.<\/p>\n\n\n\n<p>Now we get the output as the following:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nI don&#039;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.\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Getting a random quote using a random word<\/h2>\n\n\n\n<p>Now we learned about generating a keyword and quotes using different modules, let&#8217;s combine both of them and generate a quote according to a particular keyword.<\/p>\n\n\n\n<p>The code for the same is shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfrom random_word import RandomWords\nfrom quote import quote\n\nr = RandomWords()\nw = r.get_random_word()\nprint(&quot;Keyword Generated: &quot;,w)\n\nres = quote(w, limit=1)\nfor i in range(len(res)):\n    print(&quot;\\nQuote Generated: &quot;,res&#x5B;i]&#x5B;&#039;quote&#039;])\n<\/pre><\/div>\n\n\n<p>And the result is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nKeyword Generated:  fenman\n\nQuote Generated:  The fenman gazed at Wimsey with a slow pity for his bird-witted feebleness of mind.\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Today we learned generating random keyword and quotes around the keyword using Python programming Language.<\/p>\n\n\n\n<p>You can try out by generating multiple quotes on multiple keywords as well! Happy coding!<\/p>\n\n\n\n<p>Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello there today let&#8217;s learn how to get quotes from various authors using the quote module in python. So let&#8217;s begin! Generating a random keyword To get quotes from various backgrounds we will generate a random keyword every time and the program will return a quote from a particular author revolving around the keyword. To [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":16294,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-16288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/16288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=16288"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/16288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/16294"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=16288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=16288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=16288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}