×

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

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

Python List extend() method

Python List extend() method The list.extend() method extends the list by appending all the items from the iterable. Syntax list.extend(iterable) Parameter iterable: It is a required parameter which represents any iterable unlike list, set, tuple, etc. Example 1 # Python...

1 minute read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python program to check whether a given number is prime or not

Python program to check whether a given number is prime or not A positive integer greater than 1 is called a prime number if it is divisible by one and number...

1 minute read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 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.

_name_ in Python

Introduction: The code at level 0 indentation is to be performed when the command to run a Python program is supplied to the interpreter because Python does not have a main() function...

4 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.