The Python chr()
function is a built-in Python function that returns the string representing a character whose Unicode is an integer.
Sometimes we need to convert an ASCII character to its corresponding character and for such cases Python chr()
function is used.
chr(i)
As you can see above, the chr()
function takes a single parameter and returns the corresponding character of the integer ASCII value.
For example, chr(97)
will return the character a as the ASCII value of a
is 97
.
The valid range of the integer i
is from 0 to 1,114,111 (0x10FFFF
in base 16). ValueError
is raised when i
is out of this range.
>>> print(chr(98))
b
>>> print(chr(555))
ȫ
>>> print(chr(2000))
ߐ
>>> print(chr(-2))
Traceback (most recent call last):
..........
print(chr(-2))
ValueError: chr() arg not in range(0x110000)
As you can see in above example, Python interpreter throws a ValuError
when an integer out of range is supplied to the function chr()
.