×

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] 

Related Topics

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python Operator Precedence

Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it. According to one definition, the operator is...

3 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Python Random shuffle( ) method

The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of...

3 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Python Prime factorization

Python Prime factorization In this tutorial, we will design a program where we will find all the prime factors of a number. Then, we will print all these prime factors of...

3 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.