Python ord() function

Python ord() function

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

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 :

ord(ch)

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:

Example 1:

print(ord('A'))
print(ord('a'))


Output :

65
97

Example 2:

print(ord('Aa'))


Output :

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.

Example 3:

unicode="*"
print(ord(unicode))


Output :

42

Example 4:

unicode="**"
print(ord(unicode))


Output :

Traceback (most recent call last):
    print(ord(unicode))
TypeError: ord() expected a character, but string of length 2 found

Example 5:

unicode = ord("^")
print(unicode)


Output :

94

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!