How to use the Python ord() Function
The ord()
function in Python converts characters to an integer representation. In the case of Unicode characters, this is a Unicode code point and for ASCII chars it is a 7-bit ASCII code.
ord() Syntax
ord()
accepts one parameter; the character string.
ord(character)
Let's try converting some ASCII, Unicode and Emoji characters to demonstrate how it works:
print(ord('a'))
print(ord('b'))
print(ord('Æ'))
print(ord('😭'))
97
98
198
128557
To convert int representations back to a character, use the chr()
function like this:
print(chr(97))
print(chr(98))
print(chr(198))
print(chr(128557))
a
b
Æ
😭