chr() Builtin Function


Python - chr()

Python chr() builtin function is used to convert a given Unicode code point to a character.

In this tutorial, you will learn the syntax of chr() function, and then its usage with the help of example programs.

Syntax

The syntax of chr() function is

chr(x)

where

ParameterDescription
xAn integer value representing a Unicode code point

x must be in the range(0x110000).

Examples

1. Convert Unicode code point to character

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}')

Output

x      : 65
chr(x) : A

2. 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}')

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.