{"id":21063,"date":"2021-07-23T16:12:00","date_gmt":"2021-07-23T16:12:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=21063"},"modified":"2021-08-04T16:12:20","modified_gmt":"2021-08-04T16:12:20","slug":"convert-number-to-words","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/convert-number-to-words","title":{"rendered":"Convert a number to words [digit by digit] in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this tutorial, we are going to learn how to convert a number to its wording (digit-wise). For instance, if the number is 12, the wordings will be &#8220;one-two&#8221;. A similar thing will be done for the rest of the inputs.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Implementation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We would be following a number of steps which are mentioned below:<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Creating a Global list for digit to word mapping<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a global list containing wordings for each digit from 0 to 9. The list will contain elements mapped to the index as shown in the table below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Index<\/td><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><td>5<\/td><td>6<\/td><td>7<\/td><td>8<\/td><td>9<\/td><\/tr><tr><td>Wording \/ Value<\/td><td>zero<\/td><td>one<\/td><td>two<\/td><td>three<\/td><td>four<\/td><td>five<\/td><td>six<\/td><td>seven<\/td><td>eight<\/td><td>nine<\/td><\/tr><\/tbody><\/table><figcaption>Global list for digit to word mapping<\/figcaption><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\n# Global Array storing word for each digit\narr = &#x5B;&#039;zero&#039;,&#039;one&#039;,&#039;two&#039;,&#039;three&#039;,&#039;four&#039;,&#039;five&#039;,&#039;six&#039;,&#039;seven&#039;,&#039;eight&#039;,&#039;nine&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Taking the input of the number and creating the main function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To take input of the number we will make use of <code>input<\/code> function and then typecast it to integer and also we will create an empty function that will convert our number to words digit-wise.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\n# Global Array storing word for each digit\narr = &#x5B;&#039;zero&#039;,&#039;one&#039;,&#039;two&#039;,&#039;three&#039;,&#039;four&#039;,&#039;five&#039;,&#039;six&#039;,&#039;seven&#039;,&#039;eight&#039;,&#039;nine&#039;]\n\ndef number_2_word(n):\n    pass\n\nn = int(input())\nprint(&quot;Number Entered was : &quot;, n)\nprint(&quot;Converted to word it becomes: &quot;,end=&quot;&quot;)\nprint(number_2_word(n))\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Coding the Main Logic Inside the Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For this code, we will be making use of <strong>Recursion<\/strong>. If you have very little or no knowledge about Recursion I would recommend you to check out the tutorial mentioned below:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Read more on Recursion: <a href=\"https:\/\/www.askpython.com\/python\/python-recursion-function\" target=\"_blank\" rel=\"noreferrer noopener\">Recursion in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For every recursive call, we will check if my number became 0, if it did we would return an empty string otherwise we will keep adding the wordings for each digit with the help of the <strong>modulus function <\/strong>and divide the number <strong>by 10<\/strong> to shrink the number and move to the next digit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code implementation is shown below and comments are added for your understanding. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\n# Global Array storing word for each digit\narr = &#x5B;&#039;zero&#039;,&#039;one&#039;,&#039;two&#039;,&#039;three&#039;,&#039;four&#039;,&#039;five&#039;,&#039;six&#039;,&#039;seven&#039;,&#039;eight&#039;,&#039;nine&#039;]\n\ndef number_2_word(n):\n\n    # If all the digits are encountered return blank string\n    if(n==0):\n        return &quot;&quot;\n    \n    else:\n        # compute spelling for the last digit\n        small_ans = arr&#x5B;n%10]\n\n        # keep computing for the previous digits and add the spelling for the last digit\n        ans = number_2_word(int(n\/10)) + small_ans + &quot; &quot;\n    \n    # Return the final answer\n    return ans\n\nn = int(input())\nprint(&quot;Number Entered was : &quot;, n)\nprint(&quot;Converted to word it becomes: &quot;,end=&quot;&quot;)\nprint(number_2_word(n))\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Outputs<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nNumber Entered was :  123\nConverted to word it becomes: one two three\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nNumber Entered was :  46830\nConverted to word it becomes: four six eight three zero \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So by end of this tutorial, we saw that the numbers can easily be converted to the wording (digit-wise) in a pretty easy and simple way by the use of Recursion. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you for reading! Happy Learning! &#x1f607;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to learn how to convert a number to its wording (digit-wise). For instance, if the number is 12, the wordings will be &#8220;one-two&#8221;. A similar thing will be done for the rest of the inputs. Code Implementation We would be following a number of steps which are mentioned below: [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":21070,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-21063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/21063","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=21063"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/21063\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/21070"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=21063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=21063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=21063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}