Convert Numbers to Words in Python with num2words
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This snippet demonstrates how to spell out numbers as words in Python using the num2words library. After importing the function, several output modes are shown.
- Default:
num2words(36)returnsthirty-six. - Localization: passing
lang='fa'returns the Persian text «سی و شش». - Ordinals:
to='ordinal'returnsthirty-sixth, whileto='ordinal_num'returns36th. - Currency:
to='currency'returnszero euro, thirty-six cents; withnum2words(1500, lang='fa', to='currency')it returns «هزار و پانصد تومان».
Install it first with pip install num2words, then combine the lang and to arguments to get the exact output you need. This is useful for invoices, cheque amounts, text-to-speech, and any user-facing text where numbers should be readable.