Python chr() Function
The chr()
function converts an integer to its unicode character and returns it.
note
You can convert it back to unicode using the ord()
function.
Syntax
chr(number)
chr() Parameters
Python chr()
function parameters:
Parameter | Condition | Description |
---|---|---|
number | Required | An integer representing a valid Unicode code point |
note
The valid range for the number is from 0
to 1,114,111
(0x10FFFF
).
chr() Return Value
Python chr()
function returns a unicode character of the corresponding integer argument (in the range 0 to 1,114,111).
danger
Notice that it may raise an exception:
ValueError
: for an out of range integer numberTypeError
: for a non-integer argument
Examples
Example 1: chr() function with Integer Numbers
print(chr(51)) # 51 to unicode = 3
print(chr(65)) # 65 to unicode = A
print(chr(1200)) # 1200 to unicode = Ұ
output
3
A
Ұ
Example 2: chr() function with an Out-of-Range Integer
print(chr(-1000))
print(chr(1114113))
Both of them will raise a ValueError
exception as follows
ValueError: chr() arg not in range(0x110000)
Example 3: chr() function with Non-Integer Arguments
print(chr('Tom'))
print(chr('Ryan'))
Both of them will raise a TypeError
exception as follows
TypeError: an integer is required (got type str)