
My name is Jasper Camber Holton. I write, code, make music, and I am the developer of Uglek.com.
Has 335 posts, follows 17 users and is followed by 12 users.
Last seen at May 24, 2022 10:44




How to Generate a String from a Number in Python I use the following code to generate a string from a number under 1000. It is using simple arrays and if statements to generate a compound number as a string.
import math
n = ['one','two','three','four','five', 'six', 'seven', 'eight', 'nine', 'ten']
tn = ['eleven','twelve','thir','four','fif','six','seven','eigh','nine']
nn = ['teen','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety','hundred']
def number_to_string(num):
if num < 11:
return n[num-1]
if num < 20:
if num < 13:
return tn[num-11]
return tn[num-11] + 'teen'
if num < 100:
extra = '-'+n[num%10-1]
if num%10 == 0:
extra = ''
return nn[math.floor(num/10)-1]+extra
if num < 1000:
extra = '-'+n[num%10-1]
if num%10 == 0:
extra = ''
return n[math.floor(num/100)-1]+'-hundred-'+nn[math.floor((num%100)/10)-1]+extra
return 'number too large to compute!'
for x in range(1,1000):
print(number_to_string(x))

Baby geese by Greenlake! This time of year the geese are hatched and are running around in fluffy formation.
@AussieinthePNW, @Wen, like this,

Sitting down by the trail A photo from last summer, out hiking with @AussieinthePNW.

@Jasper_Holton, likes this,
© Uglek, 2022