Open In App

Convert numeric words to numbers

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The goal is to convert numeric words (such as "zero", "one", "two", etc.) into their corresponding digit forms (e.g., "0", "1", "2") to facilitate numerical operations. For example, in the string "zero four zero one", we aim to convert it into the string "0401". Let's explore different approaches to achieve this conversion.

Using dictionary mapping

This method utilizes a dictionary of word-to-digit mappings and a generator expression to convert each word in the input string to its corresponding digit. The string is split into words, and each word is directly looked up in the dictionary. The results are then joined to form the final digit string.

Python
d  = {"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4", 
      "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9"}

s = "zero four zero one"
res = ''.join(d[i] for i in s.split())

print(res) 

Output
0401

Explanation:

  • s.split() splits the string into a list of words ["zero", "four", "zero", "one"].
  • d[i] for i in s.split() looks up each word in the dictionary and retrieves the corresponding digit.
  • ''.join(...) concatenates all the digits into a single string "0401".

Using re.sub()

This method uses regular expressions re.sub to search for numeric words in the input string and replace them with their respective digits using a dictionary. It ensures only whole word matches are replaced, thanks to word boundaries (\b).

Python
import re

d = {"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4", 
    "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9"}

s = "zero four zero one"
res = ''.join(re.sub(r'\b(?:' + '|'.join(d.keys()) + r')\b', lambda match: d[match.group(0)], s).split())

print(res)

Output
0401

Explanation:

  • re.sub(...) finds and replaces full number words using regex and the dictionary d.
  • d[match.group(0)] maps each matched word to its digit.
  • ''.join(... .split()) removes spaces and joins digits into the final string.

Using replace()

This approach loops through the dictionary and uses the str.replace() method to replace every occurrence of a numeric word in the string with its digit. Once all replacements are done, it removes any remaining spaces to get the final number string.

Python
d = {"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4", 
            "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9"}
s = "four zero one four"

for i, j in d.items():
    s = s.replace(i,j)
res = s.replace(" ", "")

print(res)

Output
4014

Explanation:

  • for loop replaces each number word in the string s with its digit using str.replace().
  • After all replacements, s becomes a mix of digits and spaces.
  • s.replace(" ", "") removes spaces to form the final digit string.

Using for loop

This method manually splits the string into words, iterates through each word, looks it up in the dictionary, and builds the final result string by concatenating the digits.

Python
d = {"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4", 
            "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9"}
s = "zero four zero one"
res = ""

for i in s.split():
    res += d[i]
    
print(res) 

Output
0401

Explanation:

  • s.split() splits the input string into a list of number words.
  • for loop iterates through each word and appends its corresponding digit from dictionary d to res.

Next Article
Practice Tags :

Similar Reads