Convert Letters to Numbers and vice versa in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Convert letters to numbers in Python
  2. Convert numbers to letters in Python
  3. Convert all letters in a string to numbers

# Convert letters to numbers in Python

Use the ord() function to convert a letter to a number, e.g. number = ord('a').

The ord() function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

main.py
number = ord('a') print(number) # ๐Ÿ‘‰๏ธ 97 one_based = ord('a') - 96 print(one_based) # ๐Ÿ‘‰๏ธ 1 # ------------------------------------ number = ord('A') print(number) # ๐Ÿ‘‰๏ธ 65 one_based = ord('A') - 64 print(one_based) # ๐Ÿ‘‰๏ธ 1

convert letters to numbers

The code for this article is available on GitHub

If you need to convert an integer to its letter equivalent, click on the following subheading:

If you need to convert all letters in a string to numbers, scroll down to the next subheading.

The ord() function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

main.py
print(ord('a')) # ๐Ÿ‘‰๏ธ 97 print(ord('b')) # ๐Ÿ‘‰๏ธ 98

If you need to get a one-based result instead of the Unicode code point of the character, subtract 96 for lowercase characters or 64 for uppercase characters.

main.py
number = ord('a') print(number) # ๐Ÿ‘‰๏ธ 97 one_based = ord('a') - 96 print(one_based) # ๐Ÿ‘‰๏ธ 1 # ------------------------------------ number = ord('A') print(number) # ๐Ÿ‘‰๏ธ 65 one_based = ord('A') - 64 print(one_based) # ๐Ÿ‘‰๏ธ 1

get one based result

The code for this article is available on GitHub

The lowercase letter a has a Unicode code point of 97, so subtracting 96 gives us a value of 1.

The chr function is the inverse of ord().

main.py
print(chr(97)) # ๐Ÿ‘‰๏ธ 'a' print(chr(98)) # ๐Ÿ‘‰๏ธ 'b'

It takes an integer that represents a Unicode code point and returns the corresponding character.

# Convert numbers to letters in Python

Use the chr() function to convert a number to a letter, e.g. letter = chr(97).

The chr() function takes an integer that represents a Unicode code point and returns the corresponding character.

main.py
# โœ… convert number to letter (standard) letter = chr(97) print(letter) # ๐Ÿ‘‰๏ธ 'a' letter = chr(65) print(letter) # ๐Ÿ‘‰๏ธ 'A' # -------------------------- # โœ… convert number to letter (starting at 1) letter = chr(ord('`') + 1) print(letter) # ๐Ÿ‘‰๏ธ 'a' letter = chr(ord('@') + 1) print(letter) # ๐Ÿ‘‰๏ธ 'A'

convert numbers to letters

The code for this article is available on GitHub

The chr() function takes an integer that represents a Unicode code point and returns the corresponding character.

For example, lowercase a has a Unicode code point of 97 and uppercase A has a Unicode code point of 65.

main.py
letter = chr(97) print(letter) # ๐Ÿ‘‰๏ธ 'a' print(chr(98)) # ๐Ÿ‘‰๏ธ 'b' letter = chr(65) print(letter) # ๐Ÿ‘‰๏ธ 'A' print(chr(66)) # ๐Ÿ‘‰๏ธ 'B'

If you need to convert the number 1 to lowercase or uppercase a, use the ord() function in conjunction with chr().

main.py
letter = chr(ord('`') + 1) print(letter) # ๐Ÿ‘‰๏ธ 'a' letter = chr(ord('@') + 1) print(letter) # ๐Ÿ‘‰๏ธ 'A' print(ord('`')) # ๐Ÿ‘‰๏ธ 96 print(ord('@')) # ๐Ÿ‘‰๏ธ 64

The ord function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

main.py
print(ord('a')) # ๐Ÿ‘‰๏ธ 97 print(ord('b')) # ๐Ÿ‘‰๏ธ 98

We used the ord() function to get the Unicode code point of the tilde character because it is the last character before the lowercase letter a and added 1 to the result.

main.py
letter = chr(ord('`') + 1) print(letter) # ๐Ÿ‘‰๏ธ 'a' letter = chr(ord('@') + 1) print(letter) # ๐Ÿ‘‰๏ธ 'A' print(ord('`')) # ๐Ÿ‘‰๏ธ 96 print(ord('@')) # ๐Ÿ‘‰๏ธ 64

The @ symbol is the last character before the capital letter A, so adding 1 to the result and calling the chr() function gets us the capital letter A.

You can use a list comprehension if you need to get a list of some or all of the letters in the alphabet.

main.py
list_of_lowercase_letters = [ chr(i) for i in range(ord('a'), ord('z') + 1) ] # ๐Ÿ‘‡๏ธ ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] print(list_of_lowercase_letters) list_of_uppercase_letters = [ chr(i) for i in range(ord('A'), ord('Z') + 1) ] # ๐Ÿ‘‡๏ธ ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] print(list_of_uppercase_letters)

We used the range() class to get a range that we can iterate over and used a list comprehension to iterate over the range.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

You can use list slicing if you need to get a slice of the list of letters.

main.py
letters = [ chr(i) for i in range(ord('a'), ord('z') + 1) ] # ๐Ÿ‘‡๏ธ ['a', 'b', 'c', 'd', 'e', 'f'] print(letters[:letters.index('f') + 1])

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

We didn't specify a start index, so the list slice starts at index 0.

# Convert all letters in a string to numbers

To convert all of the letters in a string to numbers:

  1. Use a list comprehension to iterate over the string.
  2. Use the ord() function to get the Unicode code point of each character.
  3. The new list will contain the corresponding numbers.
main.py
my_str = 'bobbyhadz' numbers = [ ord(char) - 96 for char in my_str.lower() ] print(numbers) # ๐Ÿ‘‰๏ธ [2, 15, 2, 2, 25, 8, 1, 4, 26]

convert all letters in string to numbers

The code for this article is available on GitHub

We used a list comprehension to iterate over the lowercase version of the string.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the ord() function to get the Unicode code point of the current letter and subtract 96 to get a one-based result.

If you need to get a list containing the Unicode code points of each character in the string, don't subtract 96 from each call to ord().

main.py
my_str = 'bobbyhadz' numbers = [ ord(char) for char in my_str.lower() ] print(numbers) # ๐Ÿ‘‰๏ธ [98, 111, 98, 98, 121, 104, 97, 100, 122]

The new list contains the Unicode code points of the characters in the string.

Alternatively, you can use a for loop.

# Convert all letters in a string to numbers using for loop

This is a three-step process:

  1. Use a for loop to iterate over the string.
  2. Use the ord() function to get the Unicode code point of each character.
  3. Append the results to a new list.
main.py
my_str = 'bobbyhadz' numbers = [] for char in my_str.lower(): numbers.append( ord(char) - 96 ) print(numbers) # ๐Ÿ‘‰๏ธ [2, 15, 2, 2, 25, 8, 1, 4, 26]

convert all letters in string to numbers using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the lowercase version of the string.

On each iteration, we use the ord() function to get the number that corresponds to the current letter and append the result to a list.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The method returns None as it mutates the original list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev