Built-in function

ord()

Turns one character into its Unicode code point, the whole number that stands for it, such as 65 for A. chr() goes the other way.

BeginnerEvery example below runs in your browser. Change the code and press Run.

ord(c)

Every character a computer can show, from A to € to an emoji, has a number in the Unicode standard. That number is called its code point. ord() gives you that number for one character.

It is useful for simple text tricks: comparing letters by their position, shifting letters in a secret code, or finding out what an odd invisible character in your data really is.

ord() accepts exactly one character. Give it a longer string, or an empty one, and it raises TypeError. To get the numbers for a whole word, loop over it.

Parameters

ParameterWhat it is
c
required
A string of length 1 (one character). It can also be a bytes() object of length 1.

Return value

An integer: the Unicode code point of the character (0 to 1,114,111).

Examples

1Code points of a few characters

Pythoneditable
print(ord("A"))
print(ord("a"))
print(ord("0"))
print(ord(" "))
Output
65
97
48
32

2Symbols and letters from other alphabets

Pythoneditable
symbols = {"euro": "€", "n with tilde": "ñ", "omega": "Ω", "Chinese 'you'": "你"}
for name, ch in symbols.items():
    print(name, ord(ch))
Output
euro 8364
n with tilde 241
omega 937
Chinese 'you' 20320

3Position of a letter in the alphabet

Pythoneditable
def letter_position(letter):
    return ord(letter.lower()) - ord("a") + 1

for ch in "Cab":
    print(ch, letter_position(ch))
Output
C 3
a 1
b 2

Lower-case letters a to z have the code points 97 to 122 in a row, so subtracting ord("a") gives the position.

4Find a hidden character in messy data

Text copied from web pages often contains a non-breaking space. It looks like a normal space, but its number is 160, not 32.

Pythoneditable
value = "42\u00a0kg"
print(value == "42 kg")
print([ord(ch) for ch in value])
clean = value.replace("\u00a0", " ")
print(clean == "42 kg")
Output
False
[52, 50, 160, 107, 103]
True

The two strings look the same on screen, but they are not equal until the character with code point 160 is replaced by a normal space.

Common mistakes

Passing more than one character

ord("AB") raises TypeError. Loop over the string, or use a list comprehension.

Pythoneditable
word = "Hi"
try:
    ord(word)
except TypeError:
    print("TypeError: ord() needs exactly one character")
print([ord(ch) for ch in word])
Output
TypeError: ord() needs exactly one character
[72, 105]

Passing a digit as a number

ord(7) fails: ord() wants text. And ord("7") is 55, not 7. To turn the text "7" into the number 7, use int().

Questions people ask

How do I convert a number back into a character?

Use chr(): chr(65) gives 'A'. ord() and chr() undo each other.

What is the difference between ord() and ASCII?

ASCII is an old table of 128 characters (0 to 127). Unicode keeps the same numbers for those and adds over a million more, so ord() gives the ASCII value for any ASCII character.

How do I get the code point in hex, like U+20AC?

Combine with format(): f"U+{ord('€'):04X}" gives 'U+20AC'.

Spotted a mistake on this page? Tell us