Python: ord() function

Shumaiya Akter Shammi
2 min readNov 6, 2020

--

The ord() function is a built-in function in python. It returns the number which represents the Unicode of a specific character. it can not return the Unicode of a string with more than one character.

The syntax of ord() function is: ord(character)

For example:

ord() function example

Here, we can see in the above code snippet we provide the character ‘o’ as a parameter in the ord() function then prints the value of order which holds the Unicode of the character ‘o’ which is 111.

What if we provide capital ‘O’?

ord() function example

Here, the Unicode of ‘O’ is 79.

We cannot provide any string which length more than one. This function can only take a string of length 1. The example is given below:

ord() function with a string length 2

Here, we can see that we get an error and the last line of the error is:

TypeError: ord() expected a character, but string of length 2 found

So, we can provide only one character as a parameter.

Here, we have to keep in mind that, the ord() function returns the Unicode value, not the ASCII values. But the first 128 Unicode values are the same as ASCII values.

Difference between Unicode and ASCII code:

Unicode and ASCII code both are character encoding but ASCII code uses 8-bit encoding but this 8-bit encoding is possible for extended ASCII. where Unicode uses variable bit encoding(8 bit, 16 bit, 32 bit) and ASCII represents 128 characters but Unicode represents 2097152 (2²¹) characters.

Unicode is the superset of ASCII. We can represents huge number of characters using Unicode. And ASCII code is stored as 8-bit byte where Unicode is stored as byte in sequence i.e. UTF-8, UTF-16, UTF-32.

--

--

No responses yet