Python program to print number in words
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 “one-two”. A similar thing will be done for the rest of the inputs. We would be following a number of steps which are mentioned below: 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. Step 2: Taking the input of the number and creating the main functionTo take input of the number we will make use of # Global Array storing word for each digit arr = ['zero','one','two','three','four','five','six','seven','eight','nine'] def number_2_word(n): pass n = int(input()) print("Number Entered was : ", n) print("Converted to word it becomes: ",end="") print(number_2_word(n)) Step 3: Coding the Main Logic Inside the FunctionFor this code, we will be making use of Recursion. If you have very little or no knowledge about Recursion I would recommend you to check out the tutorial mentioned below: Read more on Recursion: Recursion in Python 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 modulus function and divide the number by 10 to shrink the number and move to the next digit. The code implementation is shown below and comments are added for your understanding. # Global Array storing word for each digit arr = ['zero','one','two','three','four','five','six','seven','eight','nine'] def number_2_word(n): # If all the digits are encountered return blank string if(n==0): return "" else: # compute spelling for the last digit small_ans = arr[n%10] # keep computing for the previous digits and add the spelling for the last digit ans = number_2_word(int(n/10)) + small_ans + " " # Return the final answer return ans n = int(input()) print("Number Entered was : ", n) print("Converted to word it becomes: ",end="") print(number_2_word(n)) Outputs: Number Entered was : 123 Converted to word it becomes: one two three Number Entered was : 46830 Converted to word it becomes: four six eight three zero ConclusionSo 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. Thank you for reading! Happy Learning! 😇 View Discussion Improve Article Save Article View Discussion Improve Article Save Article Write code to convert a given number into words. For example, if “1234” is given as input, the output should be “one thousand two hundred thirty-four”. Following is the implementation for the same. The code supports numbers up to 4 digits, i.e., numbers from 0 to 9999. Idea is to create arrays that store individual parts of output strings. One array is used for single digits, one for numbers from 10 to 19, one for 20, 30, 40, 50, .. etc, and one for powers of 10. C
Java
Python3
C#
PHP
Javascript
Output 9923: nine thousand nine hundred twenty three 523: five hundred twenty three 89: eighty nine 8: eight This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Approach 2: The following code supports numbers up to 15 digits, i.e., numbers from 0 to trillions. The code prints according to the western number system. Below is the implementation: C++
Java
Python
Output Thirty Six One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Eighty Nine Ten Trillion One Hundred One Billion Ten Million One Hundred Ten Thousand One Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine Time Complexity: O(Log10(N)) How do I print numbers in words in Python?num2words module in Python, which converts number (like 34) to words (like thirty-four). Also, this library has support for multiple languages.
How do I print numbers in words?Logic to print number in words:
Extract the last digit of a given number by performing modulo division by 10 and store the result in a variable. Now create a switch case to print digit 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Remove the last digit of a number. Repeat steps 3 to 5 until the number becomes 0.
How do I print a number in a string in Python?In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .
How do I tell Python to convert integers into words?Use these two lists to create a dictionary which converts from numbers to words. This is the code I have written for this: numbers = [x for x in range(0, 10)] #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] numWords = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
|