×

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

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

Python try catch exception

The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program. Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding...

3 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Python Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Arguments and parameters in Python

Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two. Parameters: While defining a function, we...

5 minutes read.