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)
Argumentinteger iAn integer number between 0 and 1,114,111 (included) representing the Unicode number of the desired Unicode symbol.
Return ValuestringReturns a Unicode symbol as a string type of length one.

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).

Publisher Link: https://nostarch.com/pythononeliners

Unicode Table

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

Unicode characterDescriptionDec
Alatin capital letter a65
Blatin capital letter b66
Clatin capital letter c67
Dlatin capital letter d68
Elatin capital letter e69
Flatin capital letter f70
Glatin capital letter g71
Hlatin capital letter h72
Ilatin capital letter i73
Jlatin capital letter j74
Klatin capital letter k75
Llatin capital letter l76
Mlatin capital letter m77
Nlatin capital letter n78
Olatin capital letter o79
Platin capital letter p80
Qlatin capital letter q81
Rlatin capital letter r82
Slatin capital letter s83
Tlatin capital letter t84
Ulatin capital letter u85
Vlatin capital letter v86
Wlatin capital letter w87
Xlatin capital letter x88
Ylatin capital letter y89
Zlatin capital letter z90
alatin small letter a97
blatin small letter b98
clatin small letter c99
dlatin small letter d100
elatin small letter e101
flatin small letter f102
glatin small letter g103
hlatin small letter h104
ilatin small letter i105
jlatin small letter j106
klatin small letter k107
llatin small letter l108
mlatin small letter m109
nlatin small letter n110
olatin small letter o111
platin small letter p112
qlatin small letter q113
rlatin small letter r114
slatin small letter s115
tlatin small letter t116
ulatin small letter u117
vlatin small letter v118
wlatin small letter w119
xlatin small letter x120
ylatin small letter y121
zlatin small letter z122
Àlatin capital letter a with grave192
Álatin capital letter a with acute193
Âlatin capital letter a with circumflex194
Ãlatin capital letter a with tilde195
Älatin capital letter a with diaeresis196
Ålatin capital letter a with ring above197
Ælatin capital letter ae198
Çlatin capital letter c with cedilla199
Èlatin capital letter e with grave200
Élatin capital letter e with acute201
Êlatin capital letter e with circumflex202
Ëlatin capital letter e with diaeresis203
Ìlatin capital letter i with grave204
Ílatin capital letter i with acute205
Îlatin capital letter i with circumflex206
Ïlatin capital letter i with diaeresis207
Ðlatin capital letter eth208
Ñlatin capital letter n with tilde209
Òlatin capital letter o with grave210
Ólatin capital letter o with acute211
Ôlatin capital letter o with circumflex212
Õlatin capital letter o with tilde213
Ölatin capital letter o with diaeresis214
Ølatin capital letter o with stroke216
Ùlatin capital letter u with grave217
Úlatin capital letter u with acute218
Ûlatin capital letter u with circumflex219
Ülatin capital letter u with diaeresis220
Ýlatin capital letter y with acute221
Þlatin capital letter thorn222
ßlatin small letter sharp s223
àlatin small letter a with grave224
álatin small letter a with acute225
âlatin small letter a with circumflex226
ãlatin small letter a with tilde227
älatin small letter a with diaeresis228
ålatin small letter a with ring above229
ælatin small letter ae230
çlatin small letter c with cedilla231
èlatin small letter e with grave232
élatin small letter e with acute233
êlatin small letter e with circumflex234
ëlatin small letter e with diaeresis235
ìlatin small letter i with grave236
ílatin small letter i with acute237
îlatin small letter i with circumflex238
ïlatin small letter i with diaeresis239
ðlatin small letter eth240

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!