Good evening! Here's our prompt for today.
Can you convert a number to its text equivalent? This is otherwise known as number spelling
.

For example, given 1234
, we expect to get "one thousand two hundred and thirty four"
back.
Constraints
- The given number will have <=
100
digits - Let n be the number of digits
- Expected time complexity :
O(n)
- Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
def number_to_words(num):
# fill in this method
return num
​
​
import unittest
​
​
class Test(unittest.TestCase):
def test_1(self):
assert number_to_words(1234) == "one thousand two hundred and thirty four"
print(
"PASSED: assert number_to_words(1234) == 'one thousand two hundred and thirty four'"
)
​
​
if __name__ == "__main__":
unittest.main(verbosity=2)
print("Nice job, 1/1 tests passed!")
​
We'll now take you through what you need to know.
How do I use this guide?
This problem is deceivingly complicated, so let's take it step and step. Let's use a simple number, like 123
for now.
First, let's convert 123
to a string type to start our operation.
xxxxxxxxxx
123 -> "123"
Then check if it's zero (because if it is, we can just return "zero").
Next, let's make sure we've defined the building blocks that will make up the final string. They include:
units
likeone
,thirteen
, etc.tens
values liketwenty
,thirty
, etc.scales
likethousand
, andmillion
Now we account for the various scales. Most of the scaling levels come up every 3 digits. Using this code block, we can get every 3 digits as chunks.

xxxxxxxxxx
start = string.length;
chunks = [];
while (start > 0) {
end = start;
chunks.push(string.slice((start = Math.max(0, start - 3)), end));
}
All it does is start from right to left, grabbing every 3 digits and pushing them into a chunks
array.
We then adjust for the scale we're at, and make sure we have enough digits to justify that scale.
Now, here's the key. For each chunk, we process it in this manner:
- Split it into individual integers ("123" becomes [1, 2, 3])
- Add tens word if array item exists
- Add 'and' string after units or tens integer (optional)
- Add hundreds word if array item exists

Complexity of Final Solution
Let n
be the number of digits in the input num
. We iterate through the string equivalent of num
, and examining 3 digits at a time for O(n)
linear time complexity. When converting nums
to a string of length n
, we have O(n)
space complexity.
One Pager Cheat Sheet
- You can convert a number to its text equivalent using
number spelling
inO(n)
time andO(n)
space complexity, where n is the number of digits and the number of digits must not exceed100
. - We can
convert
thenumber
123 to astring type
to start our operation. - We need to define
units
,tens
andscales
for building the final string, and then divide the number into chunks of 3 digits to account for different scales. - We
iterate
throughnum
inO(n)
linear
time, andconvert
it to astring
oflength n
forO(n)
space
complexity.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx
print("Nice job, 1/1 tests passed!")
# https: //www.quora.com/How-do-I-convert-numbers-to-words-in-Python
​
ones = [
"",
"one ",
"two ",
"three ",
"four ",
"five ",
"six ",
"seven ",
"eight ",
"nine ",
"ten ",
"eleven ",
"twelve ",
"thirteen ",
"fourteen ",
"fifteen ",
"sixteen ",
"seventeen ",
"eighteen ",
"nineteen ",
]
​
twenties = [
"",
"",
"twenty ",
"thirty ",
"forty ",
"fifty ",
That's all we've got! Let's move on to the next tutorial.
If you had any problems with this tutorial, check out the main forum thread here.