Sometimes we need to convert an ASCII character to its corresponding character and for such cases Python
chr() function is used.
Python chr() Syntax
As you can see above, the
chr() function takes a single parameter and returns the corresponding character of the integer ASCII value.
chr(97) will return the character a as the ASCII value of
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.
Python chr() Example
>>> 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