chr() Builtin Function

Python – chr()

Python chr() builtin function returns a character that represents the given Unicode code point.

Syntax

The syntax of chr() function is

chr(x)

where x is an integer value representing a Unicode code point.

x must be in the range(0x110000).

Examples

chr() with valid Unicode code point as argument

In the following program, we pass a value of 65 to the chr() function. Unicode code point 65 represents the character: capital alphabet A.

Python Program

x = 65
output = chr(x)
print(f'x      : {x}')
print(f'chr(x) : {output}')
Run

Output

x      : 65
chr(x) : A

chr() with invalid Unicode code point as argument

A valid Unicode code point is in the range(0x110000). If we give a number out of this range as argument to chr(), it raises ValueError as shown in the following.

Python Program

x = 12685000
output = chr(x)
print(f'x      : {x}')
print(f'chr(x) : {output}')
Run

Output

  File "/Users/pe/Documents/workspace/python/example.py", line 2, in <module>
    output = chr(x)
ValueError: chr() arg not in range(0x110000)

Summary

In this tutorial of Python Examples, we learned the syntax of chr() function, and how to get the character for a given Unicode code point, with examples.