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.
1 2 3 4 5 6 7 8 |
ones = { 1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen" } tens = { 20: "twenty", 30: "thirty", 40: "forty", 50: "fifty", 60: "sixty", 70: "seventy", 80: "eighty", 90: "ninety" } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def convert_number_to_words(num): if 1 <= num < 20: return ones[num] elif 20 <= num <= 99: ten, remainder = divmod(num, 10) return tens[ten * 10] + ('' if remainder == 0 else '-' + ones[remainder]) elif 100 <= num <= 999: hundred, remainder = divmod(num, 100) return ones[hundred] + ' hundred' + ('' if remainder == 0 else ' and ' + convert_number_to_words(remainder)) elif 1000 <= num <= 9999: thousand, remainder = divmod(num, 1000) return ones[thousand] + ' thousand' + ('' if remainder == 0 else ' ' + convert_number_to_words(remainder)) else: return "Number out of range" |
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.
1 2 3 4 |
print(convert_number_to_words(42)) # Output: forty-two print(convert_number_to_words(512)) # Output: five hundred and twelve print(convert_number_to_words(3014)) # Output: three thousand fourteen print(convert_number_to_words(10500)) # Output: Number out of range |
The full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
ones = { 1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen" } tens = { 20: "twenty", 30: "thirty", 40: "forty", 50: "fifty", 60: "sixty", 70: "seventy", 80: "eighty", 90: "ninety" } def convert_number_to_words(num): if 1 <= num < 20: return ones[num] elif 20 <= num <= 99: ten, remainder = divmod(num, 10) return tens[ten * 10] + ('' if remainder == 0 else '-' + ones[remainder]) elif 100 <= num <= 999: hundred, remainder = divmod(num, 100) return ones[hundred] + ' hundred' + ('' if remainder == 0 else ' and ' + convert_number_to_words(remainder)) elif 1000 <= num <= 9999: thousand, remainder = divmod(num, 1000) return ones[thousand] + ' thousand' + ('' if remainder == 0 else ' ' + convert_number_to_words(remainder)) else: return "Number out of range" print(convert_number_to_words(42)) # Output: forty-two print(convert_number_to_words(512)) # Output: five hundred and twelve print(convert_number_to_words(3014)) # Output: three thousand fourteen print(convert_number_to_words(10500)) # Output: Number out of range |
Conclusion
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.