The chr Python defines the characters which have a Unicode that is an integer. The chr () function is a built-in function available in the Python library that converts any integer passed in the function into a character.
The chr () function works within a limited range. The valid range of integers that can be passed within the parameters of the function is between 0 to 1,114,111.
The integer that is passed in the function represents the Unicode code point of a particular character. Given below is the syntax used for the chr Python function.
The chr () function accepts only one parameter. It should be a variable within the valid range. Thus, if any negative integer or one greater than 1,114,111 is passed as the parameter in the chr () function, then the program will return an error. You will find ValueError displayed by the compiler.
Return Value from chr()
Based on the value of the integer that is passed in the function, chr Python will return a character or a string whose Unicode code point is the integer i.
Example 1: How chr() works?
The program below shows how you can pass different integers within the parameter of the chr Python function and obtain the corresponding character or string.
print (chr (88))
print (chr (111455))
print (chr (65))
You can try the program for yourself and find which characters or strings have Unicode 88, 111455, and 65.
Example 2: The Integer passed to chr() is out of the range
To understand and verify that the chr () function has a definite range. If any integer other than the defined range is input in the parameters, then the Value Error will be raised. Here is an example. You can insert any random integer that does not lie between 0 and 1,114,111 to test this out.
print (chr (-78))
The output will print that the argument passed for the function chr Python does not lie in the range, and hence, is invalid.
The chr Python function works opposite to the ord () function. Both these functions accept only one argument. The chr () function will print the character for the corresponding integer passed in the parameter.
On the other hand, the ord () function will require a character or string of length to be passed as an argument. It will return a value that is in the form of an integer. This numeric value refers to the Unicode or ASCII value of the character passed.
Frequently Asked Questions
What does chr () do in Python?
The chr () Python is an in-built function used to obtain any character for which the Unicode code value is known. The Unicode code value in the form of an integer is passed as an argument in the function. The function returns the corresponding character or string.
Here is an example for the same:
numb11 = [17, 38, 79]
for number in numb11:
# Convert a number based on the ASCII value to a character.
print(“ The ascii value”, number, “has character value”, letter 12);
The ascii value 17 has character value
The ascii value 38 has character value &
The ascii value 79 has character value 0
What is CHR 65?
The chr 65 refers to the character A in the Unicode code value system. Likewise, 66 represents B, and so on.
What is a char in Python?
Char in Python or any other programming language refers to characters. Characters are anything that one can type on the keyboard in one keystroke.
What is the use of word () in Python?
The ord () function in Python is used to obtain the Unicode code value of any character. It is a method that accepts a single character or a string of length one. It returns a numerical value that represents the Unicode value of the character. The ord () function is used to check for special characters within a string.