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!

Ready to Learn More Web Development?

Join my Mailing List to receive two articles per week.


I send two weekly emails on building performant and resilient Web Applications with Python, JavaScript and PostgreSQL. No spam. Unscubscribe at any time.

Continue Reading?

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

The Built-in id() Function

Learn about object identities and comparisons in Python. Discover the built-in id() function, the is and is not operators, and more.

By Christoph Schiessl on Python

Christoph Schiessl

Hi, I'm Christoph Schiessl.

I help you build robust and fast web applications.


I'm available for hire as a freelance web developer, so you can take advantage of the more than a decade of experience I have collected 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 also a skilled writer who takes pride in breaking down technical concepts into the simplest possible terms.