How to Convert Letters to Numbers in Python?

In this tutorial, I will explain how to convert letters to numbers in Python. As a Python developer in the USA, while working on an order tracking system project, I had a requirement to generate alphanumeric codes, which required converting letters to numbers. Then, I explored this topic more and added the article. Let us learn more about this topic today.

Convert Letters to Numbers in Python

Python provides several ways to convert letters to numbers. Let us see some important methods.

Method 1: Use the ord() Function

The easiest way to convert a letter to its corresponding number is by using Python’s built-in ord() function. This function returns the Unicode code point of a given character.

Example: Convert Letters to Numbers Using ord()

def letter_to_number(letter):
    return ord(letter.lower()) - 96

# Example usage with USA-specific names
names = ["Alice", "Bob", "Charlie"]
for name in names:
    print(f"Name: {name}")
    numbers = [letter_to_number(letter) for letter in name]
    print(f"Numbers: {numbers}")

Output:

Name: Alice
Numbers: [1, 12, 9, 3, 5]
Name: Bob
Numbers: [2, 15, 2]
Name: Charlie
Numbers: [3, 8, 1, 18, 12, 9, 5]

I have executed the above example code and added the screenshot below.

Convert Letters to Numbers in Python

In this example, the function letter_to_number converts each letter to its corresponding number by subtracting 96 from the Unicode value of the lowercase letter. This aligns ‘a’ with 1, ‘b’ with 2, etc.

Read Python Hello World Program

Method 2: Use a Dictionary

Another method involves creating a dictionary that maps each letter to its corresponding number. This approach can be more intuitive and allows for easy customization.

Example: Convert Letters to Numbers Using a Dictionary

def create_letter_to_number_dict():
    letters = 'abcdefghijklmnopqrstuvwxyz'
    return {letter: index + 1 for index, letter in enumerate(letters)}

letter_to_number_dict = create_letter_to_number_dict()

# Example usage with USA-specific names
names = ["David", "Eve", "Frank"]
for name in names:
    print(f"Name: {name}")
    numbers = [letter_to_number_dict[letter.lower()] for letter in name]
    print(f"Numbers: {numbers}")

Output:

Name: David
Numbers: [4, 1, 22, 9, 4]
Name: Eve
Numbers: [5, 22, 5]
Name: Frank
Numbers: [6, 18, 1, 14, 11]

I have executed the above example code and added the screenshot below.

How to Convert Letters to Numbers in Python

Here, we create a dictionary where each letter maps to its numerical position. This method is flexible and can be easily extended to include uppercase letters or special characters.

Check out How to Check if a Variable Exists in Python?

Method 3: Use List Comprehension and ASCII Values

List comprehension combined with ASCII values offers a concise way to convert letters to numbers. This method is efficient and leverages Python’s powerful list comprehension feature.

Example: Convert Letters to Numbers Using List Comprehension

def letters_to_numbers(name):
    return [ord(letter.lower()) - 96 for letter in name]

# Example usage with USA-specific names
names = ["George", "Hannah", "Isaac"]
for name in names:
    print(f"Name: {name}")
    numbers = letters_to_numbers(name)
    print(f"Numbers: {numbers}")

Output:

Name: George
Numbers: [7, 5, 15, 18, 7, 5]
Name: Hannah
Numbers: [8, 1, 14, 14, 1, 8]
Name: Isaac
Numbers: [9, 19, 1, 1, 3]

I have executed the above example code and added the screenshot below.

Convert Letters to Numbers in Python Use List Comprehension

In this example, the letters_to_numbers function uses list comprehension to iterate over each letter in the name and convert it to its corresponding number.

Read How to Add Two Variables in Python?

Handle Special Cases

Let us see some special handling cases.

1. Uppercase Letters

If you need to handle uppercase letters, you can modify the functions to account for both uppercase and lowercase letters.

def letter_to_number_extended(letter):
    return ord(letter.lower()) - 96

# Example usage with mixed-case names
names = ["John", "KATE", "Liam"]
for name in names:
    print(f"Name: {name}")
    numbers = [letter_to_number_extended(letter) for letter in name]
    print(f"Numbers: {numbers}")

2. Non-Alphabetic Characters

To handle non-alphabetic characters, you can add a condition to skip these characters or assign them a specific value.

def letter_to_number_with_non_alpha(letter):
    if letter.isalpha():
        return ord(letter.lower()) - 96
    else:
        return None  # or any other value you choose

# Example usage with names containing non-alphabetic characters
names = ["M@ry", "N!ck", "O'Neil"]
for name in names:
    print(f"Name: {name}")
    numbers = [letter_to_number_with_non_alpha(letter) for letter in name]
    print(f"Numbers: {numbers}")

Check out How to Check if a Variable is NaN in Python?

Conclusion

In this tutorial, I have explained how to convert letters to numbers in Python. I discussed using methods such as ord() function, dictionaries , and list comprehensions, you can efficiently transform text data into numerical data. I also discussed how to handle some special cases like uppercase and non-alphabetic characters.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.