0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert Words to Numbers Easily

Use the word2number library and call from word2number import w2n; w2n.word_to_num('twenty one') to convert words to numbers in Python.
📋

Examples

Input'five'
Output5
Input'one hundred twenty three'
Output123
Input'zero'
Output0
🧠

How to Think About It

To convert words to numbers, think of mapping each word like 'one', 'twenty', or 'hundred' to its numeric value. Then combine these values according to English number rules to get the final number.
📐

Algorithm

1
Get the input string of words representing a number.
2
Split the string into individual words.
3
Map each word to its numeric value using a dictionary or library.
4
Combine the numeric values following number formation rules (e.g., 'twenty one' = 20 + 1).
5
Return the final numeric value.
💻

Code

python
from word2number import w2n

text = 'one hundred twenty three'
number = w2n.word_to_num(text)
print(number)
Output
123
🔍

Dry Run

Let's trace 'one hundred twenty three' through the code

1

Input string

'one hundred twenty three'

2

Convert words to number

w2n.word_to_num('one hundred twenty three') returns 123

3

Print output

123

WordValue
one1
hundred100
twenty20
three3
💡

Why This Works

Step 1: Mapping words to numbers

The code uses w2n.word_to_num which knows the numeric value of words like 'one' or 'hundred'.

Step 2: Combining values

It adds and multiplies these values correctly, so 'one hundred twenty three' becomes 100 + 20 + 3 = 123.

Step 3: Returning the result

The final number is returned and printed as an integer.

🔄

Alternative Approaches

Manual dictionary mapping
python
def words_to_num(words):
    nums = {'zero':0,'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,
            'ten':10,'eleven':11,'twelve':12,'thirteen':13,'fourteen':14,'fifteen':15,'twenty':20,'thirty':30}
    total = 0
    for word in words.split():
        if word in nums:
            total += nums[word]
    return total

print(words_to_num('five'))
Simple but limited to small numbers and no complex rules.
Using text2num library
python
from text2num import text2num
print(text2num('two hundred and fifty'))
Supports more complex phrases but requires extra installation.

Complexity: O(n) time, O(n) space

Time Complexity

The code processes each word once, so time grows linearly with the number of words.

Space Complexity

It stores mappings and intermediate values proportional to input size.

Which Approach is Fastest?

Using word2number is fast and reliable; manual methods are simpler but limited.

ApproachTimeSpaceBest For
word2number libraryO(n)O(n)General use, accurate conversion
Manual dictionary mappingO(n)O(n)Simple small numbers, learning purpose
text2num libraryO(n)O(n)Complex phrases with conjunctions
💡
Install the word2number package with pip install word2number to convert words easily.
⚠️
Trying to convert words without a library or mapping will fail because Python does not understand number words by default.