0

Is there any way in python that we can print string equivalent of the digit entered?

For eg. if the user inputs '8' the output should be 'Eight', if the input is '15', the output should be 'fifteen'.

Any help will be appreciated. Thanks

1
  • It's technically possibly using lots of if/then/else statements and/or dictionaries Commented Jul 30, 2022 at 11:10

2 Answers 2

1

You can use the num2words library to convert numbers into words ( https://pypi.org/project/num2words )

Here is a code example that requests user input and print the output in words.

from num2words import num2words
num = input('Enter number: ')
print(num2words(num))

Sample output:

Enter number: 42
forty-two

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot for your answer @DrCorgi but I am looking forward to do it without any external library.
0

Does this answer your question:

def dict_of_numbers():

    dictionary = {}
    numbers = []
    for initialise in range(100):
        numbers.append(initialise)

    #print(numbers)

    word0 = ['zero']
    wordints = ['one', 'two', 'three','four', 'five', 'six','seven', 'eight', 'nine']
    wordteens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
    'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
    wordtens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
    rest = []
    
    for i in range(len(wordtens)):
        rest.append(wordtens[i])
        for j in range(len(wordints)):
            rest.append(wordtens[i] + "-" + wordints[j])
    
    wordListFinal = []
    wordListFinal.append(word0[0])
    for i in wordints:
        wordListFinal.append(i)
    for i in wordteens:
        wordListFinal.append(i)
    for i in rest:
        wordListFinal.append(i)
    #print(wordListFinal)

    dictionary = {numbers[i] : wordListFinal[i] for i in range(len(numbers))}

    return dictionary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.