How to convert an integer number to words in python?

前端 未结 6 604
走了就别回头了
走了就别回头了 2021-01-21 05:09

Write a function that takes an integer as input argument and returns the integer using words. For example if the input is 4721 then the function should retur

6条回答
  •  抹茶落季
    2021-01-21 05:54

    Use modules for it: https://pypi.python.org/pypi/num2words

    Also, check similar questions: How do I tell Python to convert integers into words

    You can install the modules and see how it is implemented there.

    But your problem is solved like:

    def Numbers_To_Words (number):
        dictionary = {'1': "one", '2': "two", '3': "three", '4': "four", '5': "five", '6': "six",
                '7': "seven", '8': "eight", '9': "nine", '0': "zero"}
        return " ".join(map(lambda x: dictionary[x], str(number)))
    
    
    print Numbers_To_Words(1234)
    

提交回复
热议问题