Python ord() Function Tutorial

Python Ord() Function

This is a detailed tutorial of the Python ord() Function. Learn to find the integers that represent different Unicode characters using this built-in method.

Python ord() Function

The Python built-in function ord() is used to find out a unique integer number that represents a Unicode character. In other words, in Python, every Unicode character has been assigned an integer number and using this built-in function, we can find out that integer number, directly by passing the Unicode character to this function as an argument.

Syntax

ord() Parameters

The ord() function only takes a single argument and that is the Unicode character itself for which you want to find out the corresponding integer code.

Note. You must only pass a single Unicode Character as an argument. If you will pass more than one Unicode Characters, this function will raise the following type error.

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

Here x is the number of Unicode Characters that you will pass as the argument.

ord() Return Value

The return value is an integer number that uniquely corresponds to or simply represent the provided Unicode Character.

Example

The following example illustrates how you can make use of the ord() function to find out the corresponding integer for a Unicode Character in Python.

I’ve passed the different Unicode Characters as the argument the ord() Function and here in the following screenshot, you can clearly see that this function has returned the different unique integer numbers that represent the corresponding Unicode characters.

Python Ord() Function Example

Note. The built-in function chr() is the rever of the function ord(). As ord() gives an integer from a Unicode Character, the chr() function gives a Unicode Character that corresponds to the provided integer number as the argument.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *