ord()

ord(c)
Returns: int · Added in v3.0 · Updated March 13, 2026 · Built-in Functions
built-in string unicode character

The ord function returns an integer representing the Unicode code point of a single-character string. It is the inverse of chr, which converts a code point back to a character. This function is essential for working with Unicode text, as it lets you inspect the numeric value underlying any character.

Syntax

ord(c)

Parameters

ParameterTypeDefaultDescription
cstrA string containing exactly one character. Position-only parameter.

Returns: An integer representing the Unicode code point of the character.

Examples

Basic ASCII characters

# Each ASCII character has a sequential code point
print(ord('A'))
# 65

print(ord('a'))
# 97

print(ord('0'))
# 48

Special characters

# Space character
print(ord(' '))
# 32

# Newline
print(ord('\n'))
# 10

# Tab
print(ord('\t'))
# 9

Unicode characters beyond ASCII

# Euro sign
print(ord('€'))
# 8364

# Emoji (Unicode varies by Python version)
print(ord('😀'))
# 128512

# Cyrillic letter
print(ord('Я'))
# 1071

# Chinese character
print(ord('中'))
# 20013

Converting and manipulating code points

# Get the next character
char = 'A'
next_char = chr(ord(char) + 1)
print(next_char)
# B

# Get previous character
prev_char = chr(ord('d') - 1)
print(prev_char)
# c

# Check if character is uppercase
char = 'P'
is_upper = ord(char) >= 65 and ord(char) <= 90
print(is_upper)
# True

Common Patterns

Caesar cipher

A classic use case for ord is implementing a simple Caesar cipher:

def caesar_cipher(text, shift):
    result = ""
    for char in text:
        if char.isalpha():
            base = ord('A') if char.isupper() else ord('a')
            shifted = (ord(char) - base + shift) % 26 + base
            result += chr(shifted)
        else:
            result += char
    return result

# Encrypt with shift of 3
print(caesar_cipher("HELLO", 3))
# KHOOR

# Decrypt by shifting backwards
print(caesar_cipher("KHOOR", -3))
# HELLO

Character classification without string methods

def is_digit(char):
    return ord('0') <= ord(char) <= ord('9')

def is_uppercase(char):
    return ord('A') <= ord(char) <= ord('Z')

def is_lowercase(char):
    return ord('a') <= ord(char) <= ord('z')

print(is_digit('5'))
# True
print(is_uppercase('G'))
# True
print(is_lowercase('g'))
# True

Finding the maximum character in a string

Because characters are compared by their Unicode values, you can use ord to find characters by their “alphabetic” position:

def max_char(text):
    max_ord = 0
    max_char = ''
    for char in text:
        if ord(char) > max_ord:
            max_ord = ord(char)
            max_char = char
    return max_char

print(max_char("hello"))
# o

print(max_char("python"))
# y

Creating a character frequency map

def char_frequency(text):
    freq = {}
    for char in text:
        freq[char] = freq.get(char, 0) + 1
    return freq

# Find most common character
text = "hello world"
freq = char_frequency(text)
most_common = max(freq, key=lambda c: ord(c))
print(most_common)
# l

Errors

TypeError for wrong argument type

# String of length > 1 raises TypeError
ord('ab')
# TypeError: ord() expected a character, but string of length 2 found

# Non-string raises TypeError
ord(65)
# TypeError: ord() expected string of length 1, but int found

# Empty string raises TypeError
ord('')
# TypeError: ord() expected a character, but string of length 0 found

See Also

  • built-in::chr — converts an integer code point to a single-character string (inverse of ord)
  • built-in::bin — convert an integer to a binary string
  • built-in::hex — convert an integer to a hexadecimal string
  • built-in::oct — convert an integer to an octal string
  • built-in::ascii — returns a string with printable representation, escaping non-ASCII characters