# Python Letter to Number

Python Letter to Number

In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in Python. We will also learn about the ord() function that we will use in this tutorial.

Approaches to Converting Letter into Number

Here, in this section, we will learn about the approaches that we will use in our Python program so that we can convert the given letters in their number values as defined and print the result in output. We will print the numbers corresponding to the given letters in the list form.

• For example: If we have given "code" as input, then the output we will get is [3, 15, 4, 5] in the list format, etc.

We will use the following two approaches in this tutorial to perform the conversion:

1). Using for loop iteration with ord () function

2). Using list append approach with ord () function

We can see that in both approaches, we are using the ord() function. The ord() function will help in the conversion of the letter into numbers with their respective numbers defined in Python.

Now, let's understand the implementation of these approaches by using them in a program:

Method 1: Using for loop iteration:

In this method, we will use a for loop to perform an iteration over the letters given in the program as user input. We will iterate each letter from the string, and the ord() function will append the respective number value of the given letters in a list form. We will use 'ord(letter) - 96' syntax in the initial empty list.

Let's understand this method in a better way with the help of the following example program:

Example –

``` # Taking letters as user input
lett = input ("Provide letters for conversion without using space: ")
# Defining an empty list
NumList = []
# Using for loop iteration on given numbers
for let in lett:
numbers = ord (let) - 96
NumList.append (numbers) # appending empty list with numbers
# Printing conversion result
print ("Corresponding numbers for the letters given are: ", NumList) ```

Output:

``` Provide letters for conversion without using space: slinex
Corresponding numbers for the letters given are:  [19, 12, 9, 14, 5, 24] ```

As we can see that, we have letters as input from the user in the string format. Then, the program has converted the given letters with their respective number values and printed the result in the output.

Method 2: Using list append approach:

Using the list comprehension method is a more combat approach to convert the given letters into numbers in Python. In this method, after taking the letters from the user, we will directly convert them into numbers inside the list we define. We don't have to initialize an empty list first in this method. We will use 'ord(letter) - 96' syntax directly inside the list we define.

Look at the following program to understand the implementation of this method:

Example –

``` # Taking letters as user input
lett = input ("Provide letters for conversion without using space: ")
# Using list comprehension directly on empty list
NumList = [ord (let) - 96 for let in lett] # appending list
# Printing conversion result
print ("Corresponding numbers for the letters given are: ", NumList) ```

Output:

``` Provide letters for conversion without using space: rogers
Corresponding numbers for the letters given are:  [18, 15, 7, 5, 18, 19] ```