What is the Python “ord()” Function? How to Use it?

An encoding system is a set of rules or instructions that specifies the representation of information in a digital format. Unicode is a universal character/text encoding standard that offers a distinct numeric value for each symbol, character, number, or emoji. Its usage has become very popular in numerous programming languages including Python to represent strings. It enables programmers to make strings with various characters or symbols and interact with them using the “ord()” function. 

This guide will explain the Python “ord()” function and the method of using it in Python.

Quick Outline

  1. What is the “ord()” Function in Python? 
  2. How to Utilize the “ord()” Function in Python?
  3. How to Handle Errors While Using the “ord()” Function in Python?
  4. What is the Reverse Function for the “ord()” Function in Python?

What is the “ord()” Function in Python? 

The “ord()” function is a Python built-in function that is utilized to convert a specific Unicode character into its corresponding integer/numeric value. This function takes a single character or a string of length “1” as a parameter and returns a numeric value that represents the Unicode of the provided character.

Syntax

The basic syntax of the “ord()” function is:

ord(character)

Parameter 

The “ord()” function takes a single parameter i.e. character. It can be any letter, digit, or symbol. 

Return Value 

The “ord()” function returns a numeric value that represents the Unicode of the given/specified character.

How to Utilize the “ord()” Function in Python?

To use the “ord()” function in Python, pass the desired letter, special character, digit, etc. as an argument to it and the rest of the work/functionality will be accomplished by the stated function. We have listed down some common use cases of the “ord()” function in Python:

  • Case 1: Use “ord()” Function With Letters
  • Case 2: Use “ord()” Function With Symbols/Special Characters
  • Case 3: Use “ord()” Function With Digits
  • Case 4: Use “ord()” Function With Accented Letters
  • Case 5: Use “ord()” Function With Greek Letters
  • Case 6: Use “ord()” Function With Emojis
  • Case 7: Use “ord()” Function With Whitespace Characters

Case 1: Use “ord()” Function With Letters 

The “ord()” function can be used with capital and small letters such as “A”, “M”, “H” or “b”, “j”, “y”, etc. to convert them into their Unicode code points.

For example, we are converting the capital letter “A” to its corresponding integer value: 

chr = ord("A")

print(chr)

Here:

  • ord(“A”)” function converts the character “A” to its Unicode.
  • chr” is the variable that stores the Unicode.
  • print(chr)” function displays the results (integer value).

This displays the following output:

Similarly, users can convert any capital and small letters into their Unicode code points as seen below:

print (ord("y"))

print (ord("H"))

The below output shows the integer values that represent the Unicode characters i.e. “y” and “H” respectively: 

Case 2: Use “ord()” Function With Symbols/Special Characters 

Symbols or special characters are the signs that represent an idea or object. These include full stop “.”, comma “,”, hashtag “#”, asterisk “*”, and other symbols. Users can use the “ord()” function with any symbol or special characters to convert them into their Unicode code points.

For instance, we are converting the “*” symbol to its corresponding integer value: 

chr = ord("*")

print(chr)

It can be observed that the specified symbol has been converted to its integer equivalent i.e. “42”:

Likewise, we are converting the “&” and “?” symbols to their Unicode code point:

print (ord("&"))

print (ord("?"))

This gives the following output:

Case 3: Use “ord()” Function With Digits 

Digits are mathematical numbers. The “ord()” function can be used with digits such as “3”, “8”, or “0”, etc., to convert them into their corresponding integer values.

For example, we are converting “9” to its Unicode code point:

chr = ord("9")

print(chr)

The below output displays the integer value of digit “9”:

In the same way, we are converting the “0” and “5” digits to their Unicode code point:

print (ord("0"))

print (ord("5"))

The Unicode code points of the specified digits can be seen below:

Case 4: Use “ord()” Function With Accented Letters 

Accent letters or diacritics are the squiggles and dots that are written inside, above, or below particular alphabet letters in various languages. These letters include “à”, “Ñ”, “ě”, “Ⱡ”, Õ, etc. Users can also use the “ord()” function with accented letters to convert them into their integer values.

For instance, we are converting the accented letter “à” to its corresponding integer value: 

chr = ord("à")

print(chr)

The ord() function will display the following output:

Similarly, users can convert any accented letters into their Unicode code points as seen below:

print (ord("Ⱡ"))

print (ord("ě"))

The below output shows the integer values that represent the Unicode characters i.e. “Ⱡ” and “ě” respectively: 

Case 5: Use “ord()” Function With Greek Letters  

The Greek letters, such as “β”, “λ”,  “π”, “δ”, etc. are commonly utilized in scientific and mathematical equations. For example, the alpha “α” and beta “β” symbols are used for coefficients, angles, constants, etc. The “ord()” function can be used with Greek letters to get their Unicode code points.

Here, we are converting the “β” letter to its Unicode code point:

chr = ord("β")

print(chr)

The below output displays the Unicode code point of the specified letter:

Likewise, we are converting the “λ” and “π” letters to their integer values:

print (ord("λ"))

print (ord("π"))

This gives the following output:

Case 6: Use “ord()” Function With Emojis 

The emojis are the visual representation of objects, human emotions, and various symbols, such as “🥰”, “❤”, or “👍”. These are utilized on social media platforms, text messaging, etc. For example, the “😃” emoji is a smiley emoji that is used to show happiness and positive feelings. Python enables users to use the “ord()” function with emojis to convert them into their Unicode code point.

Here, we are converting the “😍” emoji to its integer value:

chr = ord("😍")

print(chr)

Subsequently, the specified emoji has been converted into its Unicode code point:

In the same way, users can get the equivalent integer values of any emoji as seen below:

print (ord("😂"))

print (ord("👌"))

This returns the corresponding integer values of the specified emojis:

Case 7: Use “ord()” Function With Whitespace Characters

The whitespace characters are the characters that are used to represent the space on the page. For example, the “ ” is used to add a single space, and the “\t” is used to add certain white spaces. The “ord()” function can be used with whitespace characters to get their Unicode code points.

For instance, we are converting the white space character “ ” to its corresponding integer value: 

chr = ord(" ")

print(chr)

The below output displays the integer value of a single space:

Similarly, users can convert any whitespace character into their Unicode code points as seen below:

print (ord("\t"))

print (ord("\n"))

The below output displays the Unicode code points of the specified whitespace characters:

How to Handle Errors While Using the “ord()” Function in Python?

As we know the “ord()” function takes only a single character as the input/parameter. Sometimes, users may want to pass more than one character (multiple characters) in the “ord()” function to convert them to their Unicode code point. In this situation, the error named “TypeError” occurs. Moreover, if the user passes the empty string in the “ord()” function, Python will raise the same error. 

Check out the below-provided examples to see which type of error occurs and how to handle these errors:

Example 1: Passing Multiple Characters

In this example, we are passing multiple characters, such as “LinuxFoss” to the “ord()” function to check their Unicode code point:

print(ord('LinuxFoss'))

This displays the following error which indicates that the provided input string has more than one character:

In order to resolve this error, users are required to use the “for” loop to iterate over each string’s character as seen below:

s = 'LinuxFoss'
for char in s:
    print(ord(char))

By doing so, the provided string with multiple characters has been converted to their integer values:

Example 2: Passing Empty String

In this example, we are passing an empty string to the “ord()” function: 

print(ord(''))

This returns the same TypeError indicating that the user must pass a string of length “1” to the “ord()” function:

Note:ord(”)” is different from “ord(‘ ‘)”. The “ord(”)” is for the empty string while the “ord(‘ ‘)” has 1 string i.e. a single space.

What is the Reverse Function for the “ord()” Function in Python?

Since the “ord()” function converts the Unicode character to its corresponding integer value, users may want to convert the numeric value back to their Unicode character. In this situation, they can use the “chr()” function which is the reverse function of the “ord()” function in Python. It converts the Unicode code point numeric value back into its equivalent character.

For example, we have an integer value “65” and we will convert it to its corresponding Unicode character using the “chr()” function:

res = chr(65)

print(res)

This returns the Unicode character of the specified integer i.e. “A”:

Likewise, users can check the corresponding character, symbol, or emoji of any integer with the help of the “chr()” function:

print(chr(956))
print(chr(54))
print(chr(128075))

This returns the following output:

Note: Click on the provided link to access Google Colab. 

Conclusion

In Python programming language, the “ord()” function is a predefined function that converts a particular Unicode character into its corresponding integer/numeric value. It accepts a single character or a string of length “1” as a parameter and returns its equivalent integer value. This function can be used with any Latin, accented, or Greek letters, symbols, digits, or emojis to convert them into their Unicode code points. This guide has explained everything about the “ord()” function in Python.