How to Convert Between Letters and Numbers in Python (ASCII/Unicode)
This guide explains how to convert between letters (characters) and their corresponding numerical representations (ASCII or Unicode code points) in Python. We'll cover converting single characters, entire strings, and lists of characters, using the built-in ord()
and chr()
functions. We'll also discuss 0-based vs. 1-based indexing.
Converting a Letter to its Unicode Code Point (ord()
)
The ord()
function takes a single character (a string of length 1) and returns its corresponding Unicode code point (an integer):
number = ord('a')
print(number) # Output: 97
number = ord('A')
print(number) # Output: 65
ord()
will return the integer value representing the unicode code point of that character.
Zero-Based vs. One-Based Indexing
The Unicode code points returned by ord()
are not directly equivalent to "a=1, b=2, ...".
If you need a 1-based index (where a
is 1
, b
is 2
, etc.), you need to adjust the result:
one_based = ord('a') - 96 # Lowercase 'a' is 97, so subtract 96
print(one_based) # Output: 1
one_based = ord('A') - 64 # Uppercase 'A' is 65, so subtract 64
print(one_based) # Output: 1
Converting a Number to its Corresponding Letter (chr()
)
The chr()
function is the inverse of ord()
. It takes an integer (a Unicode code point) and returns the corresponding character:
letter = chr(97)
print(letter) # Output: a
letter = chr(65)
print(letter) # Output: A
To get a
from 1
, b
from 2
, etc., add the appropriate offset before calling chr()
:
letter = chr(ord('`') + 1) # 96 + 1 = 97 ('a')
print(letter) # Output: a
letter = chr(ord('`') + 2) # 96 + 2 = 98 ('b')
print(letter) # Output: b
letter = chr(ord('@') + 1) # 64 + 1 = 65 ('A')
print(letter) # Output: A
\`` has the code point of 96, and
a` has the code point of 97.@
has the code point of 64, andA
has the code point of 65.
Creating Lists of Letters
List comprehensions are perfect for generating lists of letters:
list_of_lowercase_letters = [chr(i) for i in range(ord('a'), ord('z') + 1)]
print(list_of_lowercase_letters)
# Output: ['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']
list_of_uppercase_letters = [chr(i) for i in range(ord('A'), ord('Z') + 1)]
print(list_of_uppercase_letters)
# Output: ['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']
- The code uses the
ord()
function to get the ascii code of a character, and then creates a range that can be converted to characters using thechr()
function.
Converting All Letters in a String to Numbers
Using a List Comprehension
my_str = 'tomnolan'
numbers = [ord(char) - 96 for char in my_str.lower()] # Use my_str for case sensitivity
print(numbers) # Output: [20, 15, 13, 14, 15, 12, 1, 14]
my_str.lower()
: Converts the string to lowercase for consistent results (optional, if you want case-sensitivity, omit this).[ord(char) - 96 for char in ...]
: The list comprehension iterates through each character, gets its code point, and subtracts 96 to get the 1-based index.
Using a for
Loop
my_str = 'tomnolan'
numbers = []
for char in my_str.lower():
numbers.append(ord(char) - 96)
print(numbers) # Output: [20, 15, 13, 14, 15, 12, 1, 14]
- This achieves the same result as the list comprehension, but is less concise.