# Python chr() Function

The Python `chr()` function takes one number as argument that is the specified Unicode and returns the character associated to this Unicode argument. For example, the call `chr(101)` returns the Unicode character `'e'`. The allowed range of arguments are all integers between 0 and 1,114,111 (included)—integers outside this interval will raise a `ValueError`.

Here are three examples of passed Unicode numbers transformed into Unicode characters using the `chr()` built-in function:

```>>> chr(65)
'A'
>>> chr(66)
'B'
>>> chr(8364)
'€'```

The syntax is very straightforward:

`Syntax: `chr(i)``

Here are some basic usages of the function:

```Input : `chr(65)`
Output : `'A'`

Input : `chr(66)`
Output : `'B'`

Input : `chr(8364)`
Output : `'€'````

Now, you may ask: what options do you have to pass as an integer? How does the Unicode encoding look like? Let’s dive into the Unicode table next!

Check out my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

## Unicode Table

Here’s a small part of the massive Unicode table that maps each Unicode symbol to a decimal number:

By passing the number from the third column into the `chr()` function, you obtain the associated Unicode symbol in the third column. Go ahead, try it yourself!

## Tool: Integer to Unicode in Python

How to convert an integer number to a Unicode symbol in Python? Use the chr(i) function and pass the integer number as an argument!

Exercise: Try to obtain the Unicode symbol ð from the above table by changing the code in the interactive code shell!

## ValueError: chr() arg not in range(0x110000)

If you experience the ValueError: chr() arg not in range(0x110000) message, you use the chr() function with a wrong argument i. The argument i is either smaller than 0 or larger than 1,114,111. You can fix it by passing an integer 0 <= i <= 1114111.

Here’s an example of two wrong arguments `i=-1` and `i=1114112` that cause the `ValueError`, and one correct argument `i=1114111`:

```>>> chr(0)
'\x00'
>>> chr(-1)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
chr(-1)
ValueError: chr() arg not in range(0x110000)
>>> chr(1114112)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
chr(1114112)
ValueError: chr() arg not in range(0x110000)
>>> chr(1114111)
'\U0010ffff'```

## How to Convert a Unicode Integer to a String?

To convert a Unicode number `i` to the associated Unicode symbol, use the `chr(i)` function. For example, the result of `chr(65)` is the Unicode symbol `'A'`. The inverse function is the `ord(x)` that converts Unicode symbol `'A'` back to integer `65`.

```>>> chr(65)
'A'```

## How to Convert a Unicode Symbol to an Integer?

To convert a Unicode symbol `x` to the associated Unicode integer number, use the `ord(x)` function. For example, the result of `ord('A')` is the Unicode integer `65`. The inverse function is the `chr(i)` that converts Unicode integer `65` back to Unicode symbol `'A'`.

```>>> ord('A')
65```

## Summary

The Python `chr()` function takes one number as argument that is the specified Unicode and returns the character associated to this Unicode argument.

For example, the call `chr(101)` returns the Unicode character `'e'`:

```>>> chr(101)
'e'```

The allowed range of arguments are all integers between 0 and 1,114,111 (included)—integers outside this interval will raise a `ValueError`:

```>>> chr(-1)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
chr(-1)
ValueError: chr() arg not in range(0x110000)```

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!