Skip to content

Python chr() Function

  • by

Python chr() Function is used to convert an integer to its Unicode character. This function returns a string from a Unicode code integer.

chr(number)

number: An integer representing a valid Unicode code point

ValueError – for an out-of-range integer number

TypeError – for a non-integer argument

Python chr()

Simple example code.

print(97, chr(97))
print(65, chr(65))
print(1200, chr(1200))

Output:

Python chr() Function

Printing characters for each Unicode integer in the numbers list.

numbers = [101, 111, 110]

for number in numbers:
    letter = chr(number)
    print("ASCII value", number, "is ", letter)

Output:

ASCII value 101 is e
ASCII value 111 is o
ASCII value 110 is n

The chr() only accepts integers within the valid Unicode range (0 through 1,114,111). If you pass an integer outside this range, ValueError will be raised.

Do comment if you have any doubts or suggestions on this Python function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading