Convert Digits to Words in Python

In this tutorial we are going to learn to Convert Digits to Words in Python programming language.

Posted December 15, 2018 at 7:25 PM

Example code of Converting Digits to Words in Python

In some situations, we will get a requirement like converting a digits into words. This example is prepared for this purpose.

In this example we are asking user to enter the digit and then we are converting it to words. Then word value is then printed on the console. You can save this code into a python file and then run it with Python interpreter.

Here are codes:


#https://www.python-examples.com
import math
 
digits=["zero", "one", "two","three", "four", "five","six", "seven", "eight","nine"]
n=[]
 
num=int(input("Enter the number :"))
print("number :{0}".format(num))
 
if num==0:
    print(digits[num])
else:
    while num >0:
        nextdigit =num % 10
        n.append(nextdigit)
        num = math.floor(num / 10)
    
 
for i in range(len(n)-1,-1,-1):
    print(digits[n[i]])
 


Save this program in 1.py file and then run it with following command:

python 1.py

Output:

Program will ask you to enter the number and then prints its number and in word format on the console. Here is the output of the program:


deepak@deepak-VirtualBox:~$ python 1.py
Enter the number :5
number :5
five
deepak@deepak-VirtualBox:~$ python 1.py
Enter the number :2
number :2
two
deepak@deepak-VirtualBox:~$ 

In this tutorial we have learned the program which converts digits into word in python.