chr() and ord() Functions in Python
Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for conversions. These both functions have their separate conversion functions. In this article, let us discuss the chr() function and the ord() function. We will complete these functions by understanding some example codes and references.
Understanding the Odr() function:
The ord() is a built-in function in python language that is used to convert a specific character into an integer. The ord() function will take a string argument from a single code character and outputs its integer value. Ord() function is opposite to the char() function
The syntax we use for this function is as follows:
ord(a)
now let us look at an example code
Let us take a string of length one, which will return an integer presenting the Unicode. When an argument is a Unicode object, it will be the value of an 8-bit string. So we have to make sure the input we take for conversion must be of length "1." For this example, we take the code as
code 1:
print(ord('A'))
print(ord('a'))
print(ord('@'))
print(ord('b'))
print(ord('#'))
print(ord('L'))
Output:
65
97
64
98
35
76
Code 2:
print(ord('hello'))
Output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: ord() expected a character, but string of length 5 found
>
So as we can see in code 1, we used the Unicode object and the ASCII table to get the values.

For code 2, we gave input of 5 characters, and once we ran the code, we got an error message because the ord() method only accepts single characters, not multiple characters.
For a clear picture, let us look below at the ASCII table for reference values for every input character we take.
With this, we got an idea of what the ord() function and their works.
Understanding the chr() function:
This chr() is a python's built-in function that is used for converting an integer into a special character. There is a specific range to find the value of the integer, and the range lies between 0 to 1,114,111. We have to ensure that the integer input we give must be between this range, or we will get an error in the output. This function works all opposite to the ord() function.
The syntax we use for this function is given below
String chr(n)
n is where we give the integer input of range 0 to 1,114,111.
To understand this concept better, let us look at an example code.
Code:
print(chr(3))
print(chr(5))
print(chr(88))
print(chr(100))
print(chr(40))
print(chr(150))
Output:
X
d
(
?
>
Looking at the above code, we took some integers as the values for n and got the respected conversions as the output.
Code 2:
print(chr(-5))
Output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)
In this code 2, we gave an integer that is not taken from the range of n, so we got an error as an output. It is very important to provide an integer between the range as an input.
Let us look at the Unicode character table used for giving outputs based on the input integers.

Using this table, we will understand on What bases we will get our outputs.
Hexa decimal data:
In the ord() and chr() functions, we can also pass integers in the hexadecimal format, like (base 15). We will get proper outputs only if we add a prefix to the integer. To understand this concept better, let us look at an example code
Code:
print(hex(18))
print(chr(0x16))
print(ord('\x12'))
Output:
0x12
18
This code passed the hexadecimal value in the chr() and ord() functions. And got the respective outputs.
With this, we got an idea of the chr() function and its working.
Conclusion:
So far, in this article, we have covered a few built-in functions of python, which are chr () and ord(). These functions are mainly used for conversions. We learned about the essential functions of these conversions and the working of these functions, such as the syntax of the functions and the example codes used for the chr to int and int to char conversions. Finally, we have seen about the hexadecimal data where we learned that in python, we could also use the hexadecimal data just by adding a prefix to the integer and adding the required range to it. In this concept, we also discussed an example code inserting the hexadecimal values to this chr () and ord() functions.