In this article, we will learn about the Python ord() function and we will see the different examples of the ord() function ahead. ord is the short form of ordinal.
Python ord() function return value :
The python ord() function returns the Unicode code of a given character.
What is Unicode :
Unicode coding standard provides the unique number of each character. In Unicode, data can be easily transported to many different systems without any manipulation.
You can learn more about Unicode here UNICODE.
Python ord() function Syntax :
Here ch is any character whose Unicode code you want to know.
Pay attention that ‘ch’ must be a single character not a combination of characters.
Therefore, a character whose Unicode number you want to find must have a length of 1. you will get an error if you specify a number whose length is more than 1.
Let’s understand with the help of an example:
Traceback (most recent call last): print(ord('Aa')) TypeError: ord() expected a character, but string of length 2 found
You got an error because here you passed a character of more than one length. Therefore ord() function accepts only a single character in its argument.
Traceback (most recent call last): print(ord(unicode)) TypeError: ord() expected a character, but string of length 2 found
unicode = ord("^") print(unicode)