Convert numeric words to numbers
Last Updated :
15 Apr, 2025
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)
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)
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)
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)
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.
Similar Reads
Program to convert a given number to words Given a non-negative integer n, the task is to convert the given number into its English representation according to International Number System.Examples:Input: n = 0Output: "Zero"Input: n = 123Output: "One Hundred Twenty Three"Input: n = 10245Output: "Ten Thousand Two Hundred Forty Five"Input: n =
15 min read
Program to convert a given number to words | Set 2 Write code to convert a given number into words. Examples: Input: 438237764Output: forty three crore eighty two lakh thirty seven thousand seven hundred and sixty four Input: 999999Output: nine lakh ninety nine thousand nine hundred and ninety nine Input: 1000Output: one thousand Explanation: 1000 i
13 min read
Convert textual Phone Number to 10 digit numerical number Given a string S of size N containing lowercase English letters, representing a phone number(all phone numbers will be 10 digits) in words, the task is to convert the number into digits. Repeating digits can be shortened as follows: If any digit repeats two times then in words is written as "double"
9 min read
Roman to Integer Conversion Given a string in roman form, the task is to convert this given roman string into an integer. Roman numerals are based on the symbols I, V, X, L, C, D, and M, which represent 1, 5, 10, 50, 100, 500, and 1,000 respectively. Different arrangements of these symbols represent different numbers. The roma
11 min read
String to Integer - Write your own atoi() Given a string s, the task is to convert it into integer format without utilizing any built-in functions. Refer the below steps to know about atoi() function.Examples:Input: s = "-123"Output: -123Input: s = " -"Output: 0Explanation: No digits are present, therefore 0.Input: s = " 1231231231311133"Ou
7 min read