Python ord() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, ord(c) returns the Unicode code point of the single character c as an int: ord("A") is 65 and ord("a") is 97. For ASCII characters this is the familiar ASCII value.

It is the inverse of chr(). ord() also accepts a bytes or bytearray object of length 1 and returns that byte's value.

ord() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass exactly one character.

python
code_point = ord(character)
CallWhat it does
ord("A")Returns 65.
ord("\n")Returns 10.
ord("\u00e9")Returns 233.
ord(b"A")Returns 65 for a one-byte bytes object.

To get the code points of a whole string, use a loop or [ord(c) for c in text]; list(text.encode()) gives bytes instead, which differ for non-ASCII text.

Library declarations
python
ord(c, /)
# c: str of length 1, or bytes/bytearray of length 1

Parameters

ParameterDescription
cRequired. A string of exactly one character, or a bytes/bytearray of length 1.

Return Value

Return ValueDescription
intThe code point (0 to 1,114,111), or the byte value (0 to 255).

Python Examples

Example 1 — Code Points of Common Characters

Print the number behind a capital letter, a lowercase letter, a digit, a space, and a newline.

python
for ch in ["A", "a", "0", " ", "\n"]:
    print(repr(ch), ord(ch))

How It Works

  1. One character each. Every item in the list is a string of length 1, which is what ord() requires.
  2. ASCII values. Letters, digits, and the space have the same numbers as in the ASCII table.
  3. Escape sequences. "\n" is a single newline character with code point 10.
  4. repr for clarity. Printing repr(ch) shows the space and newline instead of invisible output.

Example 2 — Digit Values, Letter Positions, and Sorting

Use differences between code points to read digits and letter positions, and sort by raw code point.

python
def digit_value(ch):
    return ord(ch) - ord("0")


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


print([digit_value(d) for d in "2024"])
print(letter_position("C"), letter_position("z"))
print(ord("a") - ord("A"))
print(sorted(["b", "B", "a", "A"], key=ord))

How It Works

  1. Digits are consecutive. "0" to "9" are 48 to 57, so subtracting ord("0") gives each digit's value.
  2. Letters are consecutive. Lowercasing first and subtracting ord("a") gives C = 3 and z = 26.
  3. Case gap. Lowercase letters sit exactly 32 code points after their uppercase forms.
  4. Sort by code point. key=ord puts all capitals (65, 66) before lowercase letters (97, 98).

Example 3 — Non-ASCII Characters, Bytes, and Errors

Look up code points beyond ASCII, read a byte value, and see what happens with two characters.

python
print(ord("\u00e9"), hex(ord("\u00e9")))
print(ord("\U0001F600"))
print(ord(b"A"))
print(chr(ord("Z")) == "Z")

try:
    ord("AB")
except TypeError:
    print("ord() needs exactly one character")

How It Works

  1. Beyond ASCII. é is code point 233, written U+00E9 in Unicode charts, which hex() confirms.
  2. Large code points. The emoji U+1F600 is 128512 in decimal.
  3. Bytes input. ord(b"A") returns the byte value 65.
  4. Round trip and errors. chr(ord("Z")) gives back "Z"; a two-character string raises TypeError.

Frequently Asked Questions

Use ord(c). ord("A") returns 65. For ASCII characters the Unicode code point and the ASCII value are the same.
chr(). chr(65) returns "A".
ord() accepts exactly one character. Loop over the string to get every code point: [ord(c) for c in "AB"].
Yes. ord("\U0001F600") returns 128512. Python strings store each code point as one character.
int("7") is simplest. ord("7") - ord("0") also gives 7 and works one character at a time.

Next: pow()

Raise a number to a power, with an optional modulus.

pow() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful