The Built-In chr()
and ord()
Functions
by Christoph Schiessl on Python
As of May 2024, the latest version of the Unicode standard is 15.1, defining a massive set of 149813 characters. Handling that many characters is clearly beyond the ability of any human, so we need different methods to deal with them. This is especially true when a particular character is more exotic and less common.
Python's solution to this problem is the built-in chr()
function. This function takes a single parameter — which must be an int
object or interpretable as an int
if it is a different type — and returns a one-character string consisting of the character identified by the given int
parameter. Furthermore, since there are way more integers than Unicode characters, the parameter must not be negative and less than 0x110000
(i.e., it must be in the range of integers that are convertible to Unicode characters).
By the way, if you are curious about what exactly "interpretable as an int
" means, you should check out my article about the built-in bin()
, oct()
and hex()
functions.
Knowing all of this, it's hardly surprising how chr()
works:
Python 3.12.3 (main, Apr 20 2024, 16:22:09) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> chr(36)
'$'
>>> chr(0x20AC)
'€'
>>> chr(0xA5)
'¥'
>>> chr(-1) # out of range: less than lower boundary
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)
>>> chr(0x110000) # out of range: greater than upper boundary
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)
>>> chr(3.14) # floats are not interpretable as integers
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
Python also has a built-in ord()
function that converts a single-character string to an int
— it's precisely the reverse of chr()
. The given str
parameter must have one character length because otherwise, the function raises a TypeError
.
Python 3.12.3 (main, Apr 20 2024, 16:22:09) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ord('$')
36
>>> ord('€')
8364
>>> ord('€') == 0x20AC
True
>>> ord('¥')
165
>>> ord('¥') == 0xA5
True
>>> ord('') # too few characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord('..') # too many characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
This is already everything for today. Thank you for reading, and see you soon!