Home » Programming » Python » Python Ord

The Python ord() Function – What it Does and How to Use It

The ord() function returns an integer representation of a Unicode character. Here’s why it does that and how it’s used.

Python ord() Function Syntax

First, here’s the syntax for the ord() function:

ord(CHAR)

Note that:

  • CHAR is the single Unicode character you wish to receive the corresponding integer representation of
  • Only a single character is allowed!

OK, But Why?

The integer returned by ord() is the Unicode code point of the character. It’s a unique number that identifies the character.

Unicode represents characters from several languages and includes symbols, emojis, and combinations of all of them.

When data is saved or transmitted, those unique numbers are what’s actually stored. Then, when the data is displayed, the unique numbers are used to look up what that character should look like when drawn on-screen.

The ord() function can be helpful if you want to check whether a character is a specific Unicode character (many look identical to the naked eye but are actually different) or if you want to check what a character is before trying to convert it to a different character set (e.g., converting Unicode to ASCII for saving to a text file for use on older systems).

Examples

Below, ord() is used to get the code point for an alphabetic character (a), numeric character (3), symbol ($), and emoji (? – penguin!):

# Print Unicode code point for character 'a'
print(ord('a'))

# Print Unicode code point for character '3'
print(ord('3'))

# Print Unicode code point for character '$'
print(ord('$'))

# Print Unicode code point for character '?'
print(ord('?'))

The above code will output:

97
51
36
128039

…Each line is the integer for the Unicode code point of the given character.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment