# Posts by @Jasper_Holton (335) Nomen mihi est Jasper Camber Holton. scribe, codice, musicam fac, et sum elit Uglek.com.

Has 335 posts, follows 17 users and is followed by 12 users.

Tandem videri at May 24, 2022 10:44   Support the Author 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))
`````` Busking by Greenlake in Seattle busk interdum ab aquilone in stagni parte et paulum pecuniae in apicibus fac a meo loco et casu violino. exigua soni exposita a meo busking hodie ut tecum hic communicemus. 