Convert a number to words [digit by digit] in Python

Feature Img Digit To Word

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.


Code Implementation

We would be following a number of steps which are mentioned below:


Step 1: Creating a Global list for digit to word mapping

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.

Index0123456789
Wording / Valuezeroonetwothreefourfivesixseveneightnine
Global list for digit to word mapping
# Global Array storing word for each digit
arr = ['zero','one','two','three','four','five','six','seven','eight','nine']

Step 2: Taking the input of the number and creating the main function

To take input of the number we will make use of input function and then typecast it to integer and also we will create an empty function that will convert our number to words digit-wise.

# 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 Function

For 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 

Conclusion

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.

Thank you for reading! Happy Learning! 😇