# 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
>>> 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
>>> 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!

### Hi, I'm Christoph Schiessl.

I'm available for hire as a freelance web developer, so you can take advantage of my more than a decade of experience working on many projects across several industries. Most of my clients are building web-based SaaS applications in a B2B context and depend on my expertise in various capacities.

More often than not, my involvement includes hands-on development work using technologies like Python, JavaScript, and PostgreSQL. Furthermore, if you already have an established team, I can support you as a technical product manager with a passion for simplifying complex processes. Lastly, I'm an avid writer and educator who takes pride in breaking technical concepts down into the simplest possible terms.

#### Here are a few more Articles for you ...

The Built-In all() Function

Learn how to use the built-in all() function in Python for boolean logic, with examples and different implementations.

By Christoph Schiessl on Python

Function Definition with Simple Parameters

Learn about functions with simple parameters in Python, including how the called can decide to use positional or keyword notation.

By Christoph Schiessl on Python

How to Return Two Values from a Python Function

Returning multiple values from a function using tuples. Understand the syntax and how to write correct type annotations.

By Christoph Schiessl on Python