Python Reference ยท Built-in Functions
Python chr() function
Reference for Python chr(): return the character for a Unicode code point, its valid range, the inverse relationship with ord() and typical uses.
The built-in chr() function takes an integer Unicode code point and returns a string containing that single character. It is the inverse of ord(): chr(ord(c)) == c for any one-character string.
Code points run from 0 to 1,114,111 (0x10FFFF), so chr() covers every Unicode character, not just ASCII.
chr(65) # 'A' chr(8364) # 'โฌ'
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
chr(i)
| Name | Description |
|---|---|
i |
An integer from 0 to 1114111 (0x10FFFF). Any object with __index__() is accepted. |
Return value: a str of length 1.
Raises: ValueError if i is outside the valid range; TypeError if i is not an integer (a float such as 65.0 is rejected).
Usage notes
- Inverse of
ord().ord("A")is65andchr(65)is"A". - Strings, not bytes.
chr(200)is the text characterร, not the byte0xC8. For bytes usebytes([200]). - Surrogates. Code points
0xD800to0xDFFFare accepted, but the resulting string cannot be encoded to UTF-8 without an error handler. - Not every code point displays. Control characters (
chr(7),chr(0)) and unassigned code points are valid but may show as blanks or boxes. Userepr()orascii()to see them. - Escapes work too.
"โฌ"and"\N{EURO SIGN}"produce the same character aschr(0x20AC)and are usually clearer in source code. - Text, not glyphs. A visible "character" such as an emoji with skin tone may be several code points;
chr()makes only one. - Letters by position.
chr(ord("a") + n)gives the n-th lowercase letter;string.ascii_lowercase[n]is simpler.