Skip to content
Tutorials Center
My learning

Topics

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.

Python
chr(65)       # 'A'
chr(8364)     # 'โ‚ฌ'

Try it

Python From numbers to characters

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.

Python A Caesar shift and invalid input

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

Python
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") is 65 and chr(65) is "A".
  • Strings, not bytes. chr(200) is the text character รˆ, not the byte 0xC8. For bytes use bytes([200]).
  • Surrogates. Code points 0xD800 to 0xDFFF are 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. Use repr() or ascii() to see them.
  • Escapes work too. "โ‚ฌ" and "\N{EURO SIGN}" produce the same character as chr(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.