Python Practice › Loop Programs › Program 27
Number in words
Write a Python program that prints each digit of a number as a word.
Input
1024
Output
one zero two four
Approach
- Map each digit character to its word with a list.
Solution
words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]n = 1024result = []for ch in str(n): result.append(words[int(ch)])print(" ".join(result))Output
one zero two four
Line by line
Line 1
The list index is the digit, so words[4] is “four”.
Line 4
Looping over str(n) gives the digits from left to right.
Learn the concept
New to this topic? Read the lesson, then come back.