Python How to Convert Number to Words Easily
Use the
num2words library in Python by installing it with pip install num2words and then call num2words(number) to convert a number to words.Examples
Input5
Outputfive
Input123
Outputone hundred and twenty-three
Input0
Outputzero
How to Think About It
To convert a number to words, think about how humans say numbers aloud by breaking them into parts like hundreds, tens, and ones, then mapping each part to its word form. Using a library like
num2words automates this process by handling all these rules internally.Algorithm
1
Get the input number.2
Use a function or library that maps the number to its word form.3
Return or print the word representation.Code
python
from num2words import num2words number = 123 words = num2words(number) print(words)
Output
one hundred and twenty-three
Dry Run
Let's trace converting 123 to words using num2words.
1
Input number
number = 123
2
Convert to words
words = num2words(123) # returns 'one hundred and twenty-three'
3
Print result
print(words) # outputs 'one hundred and twenty-three'
| Step | Value |
|---|---|
| Input | 123 |
| Converted | one hundred and twenty-three |
| Output | one hundred and twenty-three |
Why This Works
Step 1: Import library
The num2words library provides a ready function to convert numbers to words.
Step 2: Call conversion
Calling num2words(number) returns the English words for the number.
Step 3: Print output
Printing the result shows the number spelled out as words.
Alternative Approaches
Manual mapping with dictionaries
python
def number_to_words(n): ones = ['zero','one','two','three','four','five','six','seven','eight','nine'] if 0 <= n < 10: return ones[n] return 'Number out of range' print(number_to_words(5))
Simple but only works for single digits; not scalable for large numbers.
Using inflect library
python
import inflect p = inflect.engine() print(p.number_to_words(123))
Another popular library that converts numbers to words with more options.
Complexity: O(1) time, O(1) space
Time Complexity
Conversion is done by direct lookup and simple rules inside the library, so it runs in constant time.
Space Complexity
Uses fixed internal data for words, so space is constant regardless of input size.
Which Approach is Fastest?
Using num2words or inflect is fast and reliable; manual methods are slower and limited.
| Approach | Time | Space | Best For |
|---|---|---|---|
| num2words library | O(1) | O(1) | All number sizes, easy use |
| inflect library | O(1) | O(1) | Flexible word formatting |
| Manual mapping | O(1) | O(1) | Very small numbers, learning purpose |
Always install
num2words first using pip install num2words before using it.Forgetting to install the
num2words library causes import errors.