Python Practice › Loop Programs › Program 27

Number in words

MediumLoop Programs · 27 of 90

Write a Python program that prints each digit of a number as a word.

Advertisement
Input
1024
Output
one zero two four

Approach

  1. 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))
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.

More loop programs

← All 90 Loop Programs

Leave a Comment