In this tutorial we will discuss about the chr() function in python programming language with the help of examples.
Python chr() function
The built-in python function is used to return a character from an integer. In simple we can say that the python chr() function is used to return a character (string) whose unicode point is an integer. The valid range of integer varies from 0 to 1,114,111.
Syntax
chr(num)
Parameters of chr() function in python
The function in python takes only one argument [i] i.e., an integer. The valid range of integer varies from 0 to 1,114,111.
Return value of Python chr() function
The python function is used to return a character (string) whose unicode point is an integer. In simple we can say that the chr() function is used to return a character from an integer.
Example 1: How Python chr() Function works?
print(chr(101))print(chr(50))
print(chr(1110))
Output
e2
і
Here, 101 is the unicode of e, 50 is the unicode of 2, and 1110 is the unicode of i.
Example 2: Another example of Python chr() Function
num = [1100, 68, 101]for number in num:
letter = chr(number)
print("The Character of ASCII value", number, "is", letter)
Output
The Character of ASCII value 1100 is ьThe Character of ASCII value 68 is D
The Character of ASCII value 101 is e
Example 3: Integer passed to Python chr() Function is out of the range
print(chr(-3))
Output
Traceback (most recent call last):File "<string>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)
As you can see that a is raised when you run the program. This is because the argument i.e., integer is out of the valid range.