Python chr()

Python chr() builtin function takes an integer and returns a string whose value is the character represented by integer as Unicode code point.

In this tutorial, we will learn about the syntax of Python chr() function, and learn how to use this function with the help of examples.

Syntax

The syntax of chr() function is

</>
Copy
chr(i)

where

ParameterRequired/OptionalDescription
iRequiredAn integer value.

Returns

The function returns object of type string.

Examples

1. chr(i) – Create character from unicode point

In this example, we will pass an integer value of 68 to chr() builtin function. The function returns a string value with the integer 68 representing as Unicode code point. Since 68 represents Unicode code point D, chr() returns the string 'D'.

Python Program

</>
Copy
i = 68
result = chr(i)
print(f'Return Value: {result}')

Output

Return Value: D

2. chr(i) – Integer too large

In this example, we will take an integer that is too large to be a Unicode code point, and pass this integer as argument to chr() function. chr() function throws OverflowError.

Python Program

</>
Copy
i = 6685248889
result = chr(i)
print(f'Return Value: {result}')

Output

Traceback (most recent call last):
  File "d:/workspace/python/example.py", line 2, in <module>
    result = chr(i)
OverflowError: Python int too large to convert to C long

Conclusion

In this Python Tutorial, we have learnt the syntax of Python chr() builtin function, and also learned how to use this function, with the help of Python example programs.