title: Converting Numbers to Words with Inflect tags: python * [pypi page](https://pypi.org/project/inflect/) ``` import inflect p = inflect.engine() p.number_to_words(38) # 'thirty-eight' ``` ## Example Convert numerical Bible references to words: ```py #!/usr/bin/env python import sys import inflect import re e = inflect.engine() args = sys.argv[1:] def n(x): return e.number_to_words(x) for arg in args: if m := re.match(r"(\d+):(\d+)-(\d+):(\d+)$",arg): # cross chapter c1,v1,c2,v2 = map(n,m.groups()) t = f"chapter {c1}, verse {v1} to chapter {c2}, verse {v2}" elif m := re.match(r"(\d+):(\d+)-(\d+)$",arg): # verses in chapter c,v1,v2 = map(n,m.groups()) t = f"chapter {c}, verses {v1} to {v2}" elif m := re.match(r"(\d+):(\d+)$",arg): # one verse c,v = map(n,m.groups()) t = f"chapter {c}, verse {v}" elif m := re.match(r"(\d+)$",arg): # one verse, don't mention chapter c = n,m.group(1) t = f"verse {v}" else: t = f"wtf? {arg}" print(f"{arg}={t}") ``` Example ```plaintext $ ./ref_to_words 3:4-5 3:16 16 3:45-4:9 3:4-5=chapter three, verses four to five 3:16=chapter three, verse sixteen 16=verse sixteen 3:45-4:9=chapter three, verse forty-five to chapter four, verse nine ```