chr and ord#

Unicode#

Unicode is a system of systematically and consistently representing characters.

Every character has a unicode code point - an integer that can be used to represent that character.

If a computer is using unicode, it displays a requested character by following the unicode encodings of which code point refers to which character.

ORD & CHR#

ord returns the unicode code point for a one-character string.
chr returns the character encoding of a code point.

ord & chr examples#

ord will always take in a single character as a string as input and will always return the code point integer that corresponds to that character:

ord('a')
97
ord('&')
38

Conversely, chr will always take in a single integer and return the character associated with that code point integer:

chr(97)
'a'
chr(38)
'&'

Inverses#

More explicitly, ord and chr are inverses of one another. Consider the following example:

inp = 'b'
out = chr(ord(inp))

print(inp, out)
b b

Taking ord('b') returns the unicode code point assicoated with the character ‘b’, which is the number 98. But, if you then take the chr(98) you get back the original character you started with, ‘b’.