Tutorial

Python ord(), chr() functions

Clear, original guide to Python ord() and chr() with examples, error handling, and practical tips covering Unicode code points and ASCII conversion.

Drake Nguyen

Founder · System Architect

3 min read
Python ord(), chr() functions
Python ord(), chr() functions

Overview of Python ord() and chr()

Python ord() and chr() are complementary built-in functions for converting between characters and their numeric code points. ord() maps a single-character string to its integer Unicode code point, while chr() turns an integer code point back into the corresponding character. These functions are useful for tasks involving character encoding, ASCII value lookups, and simple text transformations.

Python ord() — character to integer

The ord function in Python accepts a single character (a string of length 1) and returns its Unicode code point as an integer. This is how you obtain the ASCII code or Unicode code point for a character.

ord() examples

# ord function Python examples
x = ord('A')
print(x)        # ASCII value for 'A'
print(ord('ć')) # Unicode code point for 'ć'
print(ord('$')) # ASCII value for '$'

Expected output

65
263
36

Python chr() — integer to character

The chr function converts an integer code point to the corresponding character. This is the inverse of ord(), used to reconstruct a character from its Unicode/ASCII value.

chr() examples

# chr function Python examples
print(chr(65))    # 'A'
print(chr(123))   # '{'
print(chr(36))    # '$'
print(chr(0x10FFFF)) # highest valid Unicode code point

chr() valid range and errors

chr() only accepts integers in the range 0 through 1,114,111 (0x10FFFF). Supplying a number outside this range raises a ValueError. The exact error message from Python is useful when debugging.

# Example of invalid chr() input
chr(-10)
# ValueError: chr() arg not in range(0x110000)

Using ord() and chr() together

Because chr() and ord() are opposites, you can convert a character to its numeric value and back again without loss for valid inputs. This makes them handy for tasks like shifting characters, simple ciphers, or implementing custom encoding logic.

Round-trip examples

print(chr(ord('ć')))   # returns 'ć'
print(ord(chr(65)))    # returns 65

Notes, common uses and pitfalls

  • ord() requires a single-character string; passing a longer string raises a TypeError.
  • ord() returns the Unicode code point, so results may be greater than 127 for non-ASCII characters (for example, accented letters).
  • Use ord() to convert a character to an ASCII code in Python when you know the input is within the ASCII range.
  • chr() will raise ValueError if the integer is outside 0..0x10FFFF; check ranges before calling if values are untrusted.
  • These functions are part of Python built-in functions strings and are consistent across Python 3 where strings are Unicode by default.

Further examples and practical tips

To convert a character to an integer and back in Python, combine ord() and chr() as shown above. For Unicode-aware processing, remember that UTF-8 is an encoding of Unicode code points; ord() gives you the code point value, not the byte sequence.

Tip: When manipulating numeric character offsets (for example, rotating letters), perform arithmetic on the ord() result and use chr() to obtain the new character. Always guard against out-of-range integers when using chr().

Summary

In summary, Python ord() and chr() provide a reliable, straightforward way to switch between characters and their numeric Unicode/ASCII code points. Use ord() to get the code point value and chr() to recover the character from a code point. Keep the chr() valid range (0x10FFFF) and potential ValueError in mind during implementation.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.