What is Python ord Function | ord() Examples

Many times in Python programming while doing some projects. We need to find the corresponding Unicode value of a character. So the Python ord function returns the number representing the Unicode code of a specified character.

The Python ord Function is an inbuilt function which returns an integer representing the Unicode code of the specified character. In other words, in Python, every Unicode character has been assigned an integer number. So by using Python ord() built-in function, we can find out that integer number. By directly passing the Unicode character to this function as an argument.

Before discussing anything else, it is important to first know what the Unicode encoding is

Unicode Encoding

Unicode is a character encoding standard that has widespread acceptance. Microsoft software uses Unicode at its core. Whether you realize it or not, you are using Unicode already! Basically, “computers just deal with numbers. They store letters and other characters by assigning a number for each one. Before Unicode was invented, there were hundreds of different encoding systems for assigning these numbers. No single encoding could contain enough characters.”

If you are using a legacy encoding your font conflicts with the font someone in another area of the world uses. You might have a Ű in your font while someplace else someone used a Ɛ at the same codepoint. Your files are incompatible. Unicode provides a unique number for every character and so you do not have this problem if you use Unicode. If your document calls for U+0289 it will be clear to any computer program what the character should be.

Syntax of Python ord() Function

The syntax of Python ord() is:

ord(ch)

Parameters

Parameter:

Name

Description

Required /Optional

Type

c

Any character of length 1.

Required

String

Return value of Python ord() Function

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

Python ord Function Examples

Let’s move directly to the examples and see the working of the ord() function.

Example 6: Python Program to Get the Unicode code Point Value of a File Using Python ord

In this example, we are using Python ord() Function in a File. Here we will first open the file in the read-only mode. Then we will initialize a for loop and get the Unicode of all the characters present in the file.

Must Read:

Conclusion

Python ord function takes a single character as an input or a string of length 1 and returns the integer i.e Unicode equivalent code. The computer only understands these Unicode codes, Unicode codes are converted into binary which is used by the computer to process information.

Python ord is something we need to know to effectively use Python. We can change characters by adding or subtracting offset values.

Try to run the programs on your side and let me know if you have any queries.

CATEGORIES

Python Pool is a platform where not only you can read amazing Python blogs but you can write one too. It’s an opportunity for everyone who wants to read or write interesting facts/information about python.