Number in words
MediumLoop Programs · 27 of 90
Write a Python program that prints each digit of a number as a word.
Approach
- Map each digit character to its word with a list.
Solution
Python
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))
Line by line
Line 1The list index is the digit, so words[4] is “four”.
Line 4Looping over str(n) gives the digits from left to right.
Learn the concept
New to this topic? Read the lesson, then come back.
More loop programs
← All 90 Loop Programs