Editor

Python Program to Convert a Number to Words

Intermediate · Numbers

This program writes a number out in English words, the way an amount is written on a cheque: 45207 becomes forty-five thousand two hundred seven. It works for every whole number from 0 to 999999 by splitting the number into thousands and the rest.

Program

number_to_words.py
ones = ["zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"]
def below_thousand(n):
# Words for 1 to 999 (an empty string for 0)
words = []
if n >= 100:
words.append(ones[n // 100] + " hundred")
n %= 100
if n >= 20:
words.append(tens[n // 10] + ("-" + ones[n % 10] if n % 10 else ""))
elif n > 0:
words.append(ones[n])
return " ".join(words)
num = 45207 # or: num = int(input("Enter a number from 0 to 999999: "))
if not 0 <= num <= 999999:
print("Please use a number from 0 to 999999")
elif num == 0:
print("0 in words: zero")
else:
words = below_thousand(num % 1000)
if num >= 1000:
words = (below_thousand(num // 1000) + " thousand " + words).strip()
print(f"{num:,} in words: {words}")
Output
45,207 in words: forty-five thousand two hundred seven

How it works

  1. The numbers 0 to 19 all have their own names, so they sit in the ones list, and tens holds twenty, thirty and so on.
  2. below_thousand() writes 1 to 999: the hundreds digit first, then either a tens word with a hyphen, like forty-five, or a single word for anything below 20.
  3. Any number up to 999999 splits into num // 1000, the thousands, and num % 1000, the rest, and the same function writes both parts.
  4. .strip() removes the extra space left at the end of round numbers like 5000, and {num:,} prints the number with a comma.

Other ways to write it

Digit by digit

Digit by digit
digit_words = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"]
num = 45207
words = [digit_words[int(d)] for d in str(num)]
print(num, "->", " ".join(words))
Output
45207 -> four five two zero seven

Some exercises only ask for each digit as a word, which a lookup list does in a single line.

In the Indian system, with lakh

In the Indian system, with lakh
ones = ("zero one two three four five six seven eight nine ten eleven twelve "
"thirteen fourteen fifteen sixteen seventeen eighteen nineteen").split()
tens = "- - twenty thirty forty fifty sixty seventy eighty ninety".split()
def two_digits(n):
if n < 20:
return ones[n]
return tens[n // 10] + ("-" + ones[n % 10] if n % 10 else "")
def indian_words(n): # works for 0 to 99,99,999
parts = []
places = [("lakh", 100000), ("thousand", 1000), ("hundred", 100), ("", 1)]
for name, size in places:
if n >= size:
parts.append((two_digits(n // size) + " " + name).strip())
n %= size
return " ".join(parts) or "zero"
for num in [125000, 4570890, 999]:
print(f"{num}: {indian_words(num)}")
Output
125000: one lakh twenty-five thousand
4570890: forty-five lakh seventy thousand eight hundred ninety
999: nine hundred ninety-nine

Indian numbering groups digits as thousands, then lakhs (1,00,000), so 125000 is one lakh twenty-five thousand. The loop takes each place out with // and %, largest first.

Try it yourself

Try 0, 19, 100, 1000 and 999999 to check that every branch of the program gives the right words.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises