Python 3 reference · Built-in Functions
Python ord() Function
Return the Unicode code point (an integer) of a single character, such as ord('A') giving 65. The inverse of chr().
Python 3 Since 3.0
Syntax
ord(c)
Parameters
| Parameter | Description |
|---|---|
c |
A string of exactly one character (or a bytes/bytearray of length 1). |
Return value
An int.
Examples
print(ord("A"), ord("a"), ord("€"))
print(chr(ord("A") + 2))
Output
65 97 8364 C
word = "Python"
print([ord(c) for c in word])
print(sum(ord(c) for c in word) % 100) # a toy checksum
Output
[80, 121, 116, 104, 111, 110] 42
Good to know
ord() needs exactly one character: ord("ab") and ord("") raise TypeError.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
chr()· Return the one-character string for a Unicode code point, such as chr(65) giving 'A'. The inverse of ord().ascii()· Return a printable representation of an object, like repr(), but with every non-ASCII character escaped.bytes()· Create an immutable sequence of bytes: raw binary data such as file contents, network messages or encoded text.- Python Strings · tutorial