How To Convert Number To Words In Python Using Dictionary

In this tutorial, we will learn how to convert a number to words in Python using a dictionary. This is a useful technique for tasks such as converting user inputs or data stored as numbers into a more readable format to be displayed or processed in various applications. Let’s dive into the steps required to achieve this task.

Step 1: Create a dictionary for number-to-word mapping

First and foremost, we need to create a dictionary that will map numbers to their corresponding words. This dictionary will be used to look up the words for the input number while converting it to words. Here, we create a dictionary for numbers ranging from 1 to 19, and another one containing tens place numbers like 10, 20, 30, etc.

Step 2: Define a function to convert numbers to words

Next, let’s define a function called convert_number_to_words that takes a number as input and returns its corresponding word form using the dictionaries created in the previous step. The function will handle numbers between 1 and 9999.

Step 3: Test the function

Now that our function is defined, it’s time to test it with some examples. We can call the function with different input numbers and see if it returns the expected output.

The full code


In this tutorial, we have learned how to convert a number to words in Python using a dictionary. This method can easily be extended to handle larger numbers, or even decimals, by modifying the function and adding appropriate dictionary entries.

The convert_number_to_words function can be incorporated into your Python applications to provide a more human-readable format for numerical data.