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)
Argument | integer i | An integer number between 0 and 1,114,111 (included) representing the Unicode number of the desired Unicode symbol. |
Return Value | string | Returns a Unicode symbol as a string type of length one. |
Here are some basic usages of the function:
Input :chr(65)
Output :Input :
'A'
chr(66)
Output :Input :
'B'
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 character | Description | Dec |
---|---|---|
A | latin capital letter a | 65 |
B | latin capital letter b | 66 |
C | latin capital letter c | 67 |
D | latin capital letter d | 68 |
E | latin capital letter e | 69 |
F | latin capital letter f | 70 |
G | latin capital letter g | 71 |
H | latin capital letter h | 72 |
I | latin capital letter i | 73 |
J | latin capital letter j | 74 |
K | latin capital letter k | 75 |
L | latin capital letter l | 76 |
M | latin capital letter m | 77 |
N | latin capital letter n | 78 |
O | latin capital letter o | 79 |
P | latin capital letter p | 80 |
Q | latin capital letter q | 81 |
R | latin capital letter r | 82 |
S | latin capital letter s | 83 |
T | latin capital letter t | 84 |
U | latin capital letter u | 85 |
V | latin capital letter v | 86 |
W | latin capital letter w | 87 |
X | latin capital letter x | 88 |
Y | latin capital letter y | 89 |
Z | latin capital letter z | 90 |
a | latin small letter a | 97 |
b | latin small letter b | 98 |
c | latin small letter c | 99 |
d | latin small letter d | 100 |
e | latin small letter e | 101 |
f | latin small letter f | 102 |
g | latin small letter g | 103 |
h | latin small letter h | 104 |
i | latin small letter i | 105 |
j | latin small letter j | 106 |
k | latin small letter k | 107 |
l | latin small letter l | 108 |
m | latin small letter m | 109 |
n | latin small letter n | 110 |
o | latin small letter o | 111 |
p | latin small letter p | 112 |
q | latin small letter q | 113 |
r | latin small letter r | 114 |
s | latin small letter s | 115 |
t | latin small letter t | 116 |
u | latin small letter u | 117 |
v | latin small letter v | 118 |
w | latin small letter w | 119 |
x | latin small letter x | 120 |
y | latin small letter y | 121 |
z | latin small letter z | 122 |
À | latin capital letter a with grave | 192 |
Á | latin capital letter a with acute | 193 |
 | latin capital letter a with circumflex | 194 |
à | latin capital letter a with tilde | 195 |
Ä | latin capital letter a with diaeresis | 196 |
Å | latin capital letter a with ring above | 197 |
Æ | latin capital letter ae | 198 |
Ç | latin capital letter c with cedilla | 199 |
È | latin capital letter e with grave | 200 |
É | latin capital letter e with acute | 201 |
Ê | latin capital letter e with circumflex | 202 |
Ë | latin capital letter e with diaeresis | 203 |
Ì | latin capital letter i with grave | 204 |
Í | latin capital letter i with acute | 205 |
Î | latin capital letter i with circumflex | 206 |
Ï | latin capital letter i with diaeresis | 207 |
Ð | latin capital letter eth | 208 |
Ñ | latin capital letter n with tilde | 209 |
Ò | latin capital letter o with grave | 210 |
Ó | latin capital letter o with acute | 211 |
Ô | latin capital letter o with circumflex | 212 |
Õ | latin capital letter o with tilde | 213 |
Ö | latin capital letter o with diaeresis | 214 |
Ø | latin capital letter o with stroke | 216 |
Ù | latin capital letter u with grave | 217 |
Ú | latin capital letter u with acute | 218 |
Û | latin capital letter u with circumflex | 219 |
Ü | latin capital letter u with diaeresis | 220 |
Ý | latin capital letter y with acute | 221 |
Þ | latin capital letter thorn | 222 |
ß | latin small letter sharp s | 223 |
à | latin small letter a with grave | 224 |
á | latin small letter a with acute | 225 |
â | latin small letter a with circumflex | 226 |
ã | latin small letter a with tilde | 227 |
ä | latin small letter a with diaeresis | 228 |
å | latin small letter a with ring above | 229 |
æ | latin small letter ae | 230 |
ç | latin small letter c with cedilla | 231 |
è | latin small letter e with grave | 232 |
é | latin small letter e with acute | 233 |
ê | latin small letter e with circumflex | 234 |
ë | latin small letter e with diaeresis | 235 |
ì | latin small letter i with grave | 236 |
í | latin small letter i with acute | 237 |
î | latin small letter i with circumflex | 238 |
ï | latin small letter i with diaeresis | 239 |
ð | latin small letter eth | 240 |
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.