The ord() is a built-in function in Python that returns the Unicode code from a given character. In other words, this function accepts a single char as an argument and returns the Unicode equivalence of the char.

Python ord function:

Function signature

The signature for the ord() function is as shown below. i is an integer value returned by a function.


Parameters and return type

  • This method takes a mandatory parameter character or a string of length 1.
  • However, it returns an integer representing the Unicode of the argument.

Python Built-in Function ord Examples:

Example 1: In this example, let us demonstrate the working of this function for a single character. As we can see, it will print the Unicode of the character in the argument.

#Using ord
print("Integer value returned for a is ",i)


Integer value returned for a is  97

Example 2: In this case, We will take a string and get the Unicode character of the string using for loop.

#Using ord
for i in s:
    print("Unicode character for ",i," is ",ord(i))


Unicode character for  p  is  112
Unicode character for  y  is  121
Unicode character for  t  is  116
Unicode character for  h  is  104
Unicode character for  o  is  111
Unicode character for  n  is  110

Example 3: For this example, let us take a string of length greater than 1. So, it will raise a TypeError exception.

#Using ord
print("Unicode character for s is ",ord(s))


Traceback (most recent call last):
  File "", line 3, in 
    print("Unicode character for s is ",ord(s))
TypeError: ord() expected a character, but string of length 6 found


The ord() function returns an integer that is a Unicode value of a single character. However, it will raise an exception if more than one character is passed as a parameter.


Happy Learning 🙂

Leave A Comment