In this python function program, we will create a function to convert an integer to its word format.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function to_number.
- Use the def keyword to define the function.
- Take a number as input.
- User for loop to iterate over a number after converting it into a string using str() and create an empty string.
- Add the words with respect to the number to the empty string.
- Use if-elif statements for the purpose.
- Print the new string which contains numbers in the word form.
- Call the function to get the output.
def to_number():
num = int(input("Enter a number: "))
str1 = ""
for i in str(num):
if i == "1":
str1 += "One"
elif i == "2":
str1 += "Two"
elif i == "3":
str1 += "Three"
elif i == "4":
str1 += "Four"
elif i == "5":
str1 += "Five"
elif i == "6":
str1 += "Six"
elif i == "7":
str1 += "Seven"
elif i == "8":
str1 += "Eight"
elif i == "9":
str1 += "Nine"
print(str1)
to_number()
Output :
Enter a number: 2563
TwoFiveSixThree
Related Articles
Python function program to get all permutations from a string.
Python function program to add two numbers.
Python function program to print the input string 10 times.
Python function program to print a table of a given number.
Python function program to find the maximum of three numbers.
Python function program to find the sum of all the numbers in a list.