Functions - Numbers to Text

# Spellnumber | Convert Numbers to Text in Python¶

Create a function that converts numbers to text in Python. It can also be called a spellnumber(), etc.

### The case of First five numbers¶

In [1]:
```def sp5(n) :
if n == 1 :
return 'One'
elif n == 2 :
return 'Two'
elif n == 3 :
return 'Three'
elif n == 4 :
return 'Four'
elif n == 5 :
return 'Five'

```
In [2]:
```# Run this function
sp5(4)
```
Out[2]:
`'Four'`

### The case of 1 to 10¶

In [3]:
```def sp10(n) :
if n == 1 :
return 'One'
elif n == 2 :
return 'Two'
elif n == 3 :
return 'Three'
elif n == 4 :
return 'Four'
elif n == 5 :
return 'Five'
elif n == 6 :
return 'Six'
elif n == 7 :
return 'Seven'
elif n == 8 :
return 'Eight'
elif n == 9 :
return 'Nine'
elif n == 10 :
return 'Ten'
```
In [4]:
```sp10(8)
```
Out[4]:
`'Eight'`

### Extend it to Twenty¶

In [3]:
```def sp20(n) :
if n == 1 :
return 'One'
elif n == 2 :
return 'Two'
elif n == 3 :
return 'Three'
elif n == 4 :
return 'Four'
elif n == 5 :
return 'Five'
elif n == 6 :
return 'Six'
elif n == 7 :
return 'Seven'
elif n == 8 :
return 'Eight'
elif n == 9 :
return 'Nine'
elif n == 10 :
return 'Ten'
elif n == 11 :
return 'Eleven'
elif n == 12 :
return 'Twelve'
elif n == 13 :
return 'Thirteen'
elif n == 14 :
return 'Fourteen'
elif n == 15 :
return 'Fifteen'
elif n == 16 :
return 'Sixteen'
elif n == 17 :
return 'Seventeen'
elif n == 18 :
return 'Eighteen'
elif n == 19 :
return 'Nineteen'
elif n == 20 :
return 'Twenty'
```
In [4]:
```sp20(15)
```
Out[4]:
`'Fifteen'`

# Up to 100¶

If the entered number is less than 21, we can call the sp20() function. If the number is greater than or equal to 21, then we shall split it into two parts and process them separately.

In [16]:
```def sp100(n) :

if n < 21 :
return sp20(n)

else :
strnumber = str(n)
part1 = strnumber[0]
part2 = strnumber[1]

# We can convert string back to integers with the int() function
part1 = int(part1)
part2 = int(part2)

# So if n = 35, its part1 is 3, which means thirty
# its part2 = 5, which we can convert with the function sp20 to five
# We just need to work on part1 here

if part1 == 3 :
return 'Thirty ' + sp20(part2)

elif part1 == 4 :
return 'Forty ' + sp20(part2)

elif part1 == 5 :
return 'Fifty ' + sp20(part2)

elif part1 == 6 :
return 'Sixty ' + sp20(part2)

elif part1 == 7 :
return 'Seventy ' + sp20(part2)

elif part1 == 8 :
return 'Eight ' + sp20(part2)

elif part1 == 9 :
return 'Ninety ' + sp20(part2)
```
In [21]:
```sp100(55)
```
Out[21]:
`'Fifty Five'`
In [18]:
```sp100(78)
```
Out[18]:
`'Seventy Eight'`
In [ ]:
```
```