Convert numeric words to numbers

In this program, we will take a string as input and convert numeric words to numbers.

Steps to solve the program
  1. Take a string of numeric words as input.
  2. Use For loop to iterate over each word of the string.
  3. Create an empty string.
  4. Convert the numeric word to its corresponding number and add it to the empty string.
  5. Print the output.
				
					#Input string
string = "five four three two one"
new_str = ""

for val in string.split():
    if val == "one":
        new_str += "1"
    elif val == "two":
        new_str += "2"
    elif val == "three":
        new_str += "3"
    elif val == "four":
        new_str += "4"
    elif val == "five":
        new_str += "5"
    elif val == "six":
        new_str += "6"
    elif val == "seven":
        new_str += "7"
    elif val == "eight":
        new_str += "8"
    elif val == "nine":
        new_str += "9"
    elif val == "ten":
        new_str += "10"

#Printing output        
print(new_str)
				
			

Output :

				
					54321
				
			

floating numbers up to 3 decimal places and convert it to string.

find the location of a word in a string

Leave a Comment