Python chr() Function: Convert Integers to Characters

The Python chr() function takes an integer Unicode code point and returns the one-character string it stands for. So chr(65) gives 'A', and chr(8364) gives the euro sign:

chr(65)     # 'A'
chr(8364)   # '€'

It’s the reverse of ord(), and it accepts any whole number from 0 to 1,114,111.

In this tutorial, I’ll cover the syntax, ASCII and Unicode examples, building A-Z letters, joining code lists into text and the two errors chr() raises. My test machine ran Python 3.12.5 in Command Prompt.

Python chr() syntax and return value

chr(i)

i must be an int from 0 to 0x10FFFF. You always get back a str of length 1.

Codes 0-127 are plain ASCII. To go the other way, from a letter to its number, use the Python ord() function.

Convert an integer to a character with chr()

Here are a few everyday codes, plus a receipt line that builds the dollar sign from code 36:

print(chr(65), chr(97), chr(48), chr(36))   # A a 0 $
letter = chr(66)
print(type(letter), len(letter))

price = 4.75
print("Latte " + chr(36) + f"{price:.2f}")   # $ is code 36

Output:

A a 0 $
<class 'str'> 1
Latte $4.75
Command Prompt output of the Python chr() function converting 65, 97, 48 and 36 to A, a, 0 and $
Each integer comes back as a one-character string, and the receipt line prints Latte $4.75.

Note that chr(48) is the text '0', not the number zero.

How do I print Unicode symbols and emoji with chr()?

Unicode charts list code points in hex, and Python accepts hex literals. This loop prints the euro, copyright, degree, check mark and a smiley:

codes = [0x20AC, 0xA9, 0xB0, 0x2713, 0x1F600]
for code in codes:
    print(f"{code:>6} {hex(code):>8} -> {chr(code)}")

print(f"Austin, TX: 72{chr(176)}F")
print(chr(0x10FFFF) == "\U0010FFFF")   # the last code point

Output:

  8364   0x20ac -> €
   169     0xa9 -> ©
   176     0xb0 -> °
 10003   0x2713 -> ✓
128512  0x1f600 -> 😀
Austin, TX: 72°F
True
Command Prompt output of Python chr() printing the euro sign, copyright, degree symbol, check mark and an emoji from hex code points
The euro, copyright, degree and check mark print normally in Command Prompt; the emoji shows up as a plain outline face.

Command Prompt has a catch: Python sends the right character, but the console font draws the emoji as a black-and-white outline. Windows Terminal shows the color version.

If those symbols later cause trouble in a file or API, see how to check if a string is ASCII in Python or remove non-ASCII characters in Python.

Generate letters A-Z with chr() and ord()

Pairing chr() with ord() is the classic way to count through letters. Here it builds the alphabet and a simple Caesar shift:

import string

letters = "".join(chr(n) for n in range(ord("A"), ord("Z") + 1))
print(letters)
print(letters == string.ascii_uppercase)

def shift(text, n):
    return "".join(chr((ord(c) - 65 + n) % 26 + 65) for c in text)

print(shift("TEXAS", 3), shift(shift("TEXAS", 3), -3))

Output:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
True
WHADV TEXAS
Command Prompt output of Python chr() and ord() building the A-Z alphabet and a Caesar shift of TEXAS
The generated alphabet matches string.ascii_uppercase, and shifting TEXAS by 3 gives WHADV, which shifts back to TEXAS.

For a plain alphabet, string.ascii_uppercase is shorter. You’ll want chr() when the letter depends on a number, like a shifted key.

Turn a list of character codes into a string

map(chr, codes) converts every number, and "".join() glues the pieces together.:

codes = [72, 101, 108, 108, 111, 33]
print("".join(map(chr, codes)))

print(repr(chr(10)), repr(chr(9)), repr(chr(0)))

Output:

Hello!
'\n' '\t' '\x00'

Newline, tab and null are invisible, so repr() shows them as escapes. Those escapes are explained in my guide to the escape sequence in Python, and Python ascii() function does a similar job for whole strings.

Fix ValueError and TypeError in Python chr()

A number past 1,114,111 (or below 0) raises ValueError:

print(chr(1114112))

Output:

Traceback (most recent call last):
  File "C:\pyguides\runs\chrfunc\ex_valueerr.py", line 1, in <module>
    print(chr(1114112))
          ^^^^^^^^^^^^
ValueError: chr() arg not in range(0x110000)

A string, even one that looks like a number, raises TypeError. Floats such as 65.0 fail the same way:

print(chr("65"))

Output:

Traceback (most recent call last):
  File "C:\pyguides\runs\chrfunc\ex_typeerr.py", line 1, in <module>
    print(chr("65"))
          ^^^^^^^^^
TypeError: 'str' object cannot be interpreted as an integer

For user or CSV input, call int() first and catch both errors. Converting text safely is covered in converting a string to an integer in Python:

def safe_chr(value):
    try:
        return chr(int(value))
    except (ValueError, TypeError) as err:
        return f"invalid ({err})"

for v in ["65", 9731, 1114112, "abc"]:
    print(repr(v), "->", safe_chr(v))

Output:

'65' -> A
9731 -> ☃
1114112 -> invalid (chr() arg not in range(0x110000))
'abc' -> invalid (invalid literal for int() with base 10: 'abc')
Command Prompt output of a safe_chr helper that converts valid values and reports ValueError and TypeError messages for bad ones
The valid inputs become A and a snowman, while 1114112 and “abc” return the error message instead of crashing.

You might find these character guides useful too:

Frequently asked questions

What does chr() do in Python?

It returns the character for a code point: chr(97) returns 'a'.

What is the range of chr() in Python 3?

0 to 1,114,111 (0x10FFFF). Anything else raises ValueError.

How do I convert an ASCII value to a character in Python?

Pass the ASCII code to chr(): chr(72) is 'H'.

What is the difference between chr() and ord()?

chr() turns a number into a character; ord() does the reverse.

Can chr() take a string like “65”?

No, it raises TypeError. Use chr(int("65")).

Where is chr() documented?

See chr() in the Python built-in functions docs.