HOWTO · Python

How to Convert Int to ASCII in Python

This tutorial demonstrates how to convert int to ASCII in Python.

On this page

In Python 3, call chr(code_point) to turn an integer Unicode code point into a one-character string. The result is an ASCII character only when the value is in the standard ASCII range, 0 through 127; larger values can produce Unicode characters outside ASCII. For example, chr(65) returns A. Standard ASCII assigns values 0–127, not 256 values, so “integer to ASCII” is precise only when the integer is in that range.

Convert an Integer to a Character with chr()

Use chr() when your integer represents a character’s code point. The returned value is a Python string containing one character. Run this example:

code_point = 65
character = chr(code_point)
print(character)
print(ord(character))
A
65

The first line of output is the character for code point 65. The second shows that ord() maps that single character back to its code point. For ASCII characters, this number is also the ASCII value; for other characters it is a Unicode code point, not an ASCII value.

The same operation works beyond ASCII. For instance, chr(233) returns é; its code point is outside the ASCII range. If you have several code points, apply chr() to each and join the resulting characters:

code_points = [72, 105, 33]
message = "".join(chr(value) for value in code_points)
print(message)
Hi!

Python 3 accepts Unicode code points from 0 through 0x10FFFF, inclusive. A value outside that range causes chr() to raise ValueError. The upper limit is inclusive: chr(0x10FFFF) is valid, while chr(0x110000) is outside the range. Check untrusted values before conversion if your program should handle them gracefully.

Convert Integers with String Formatting

Character formatting is another way to turn an integer into a character. The %c format and the :c format specifier in an f-string both format an integer as its corresponding character. They do not perform a different kind of ASCII encoding: the same ASCII range distinction still applies.

Use %c when maintaining code that uses percent formatting:

code_point = 66
print("%c" % code_point)
B

For an f-string, put :c after the integer expression:

code_point = 67
print(f"{code_point:c}")
C

These formats are useful when building a larger string. Choose either style based on the surrounding code; use chr() when you want to make the code point to character conversion explicit or need to transform a collection of values. Each approach produces a character from an integer, so values intended to represent ASCII should still be in 0–127.

Python 2: Use unichr() and Convert Characters Back with ord()

unichr() is the Python 2 legacy counterpart for converting a Unicode code point to a Unicode character. Python 2 also has chr(), which is for byte values, so unichr() is the relevant name when the intended result is a Unicode character. Python 3 strings are Unicode, and chr() handles Unicode code points directly; use it for new Python 3 code rather than the legacy function.

In Python 3, ord(character) takes a string containing exactly one character and returns its Unicode code point. Thus ord("A") is 65, while the result for a non-ASCII character is its Unicode code point. For a single valid code point and its character, ord(chr(value)) recovers value, and chr(ord(character)) recovers character. These are character/code-point operations, not general byte encoders or decoders. If you need bytes for a particular encoding, encode the string explicitly and choose that encoding for your data format.