Python BasicsEasy

Convert digit/number to words

Detailed guide and Python implementation for the 'Convert digit/number to words' problem.

Problem Statement

Easy

Write a function number_to_words(n) that takes a non-negative integer n and returns a string where each digit is converted to its English word, separated by spaces.

Digit-word mapping: 0='Zero', 1='One', 2='Two', 3='Three', 4='Four', 5='Five', 6='Six', 7='Seven', 8='Eight', 9='Nine'.

For example, 123 → 'One Two Three'.

Constraints
  • 0 <= n <= 10^9

Examples

Example 1
Input
number_to_words(123)
Output
'One Two Three'
Explanation

1 → 'One', 2 → 'Two', 3 → 'Three'. Joined with spaces.

Example 2
Input
number_to_words(405)
Output
'Four Zero Five'
Explanation

4 → 'Four', 0 → 'Zero', 5 → 'Five'.

Example 3
Input
number_to_words(0)
Output
'Zero'
Explanation

Single digit 0 → 'Zero'.

Need a Hint?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.