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:
- 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).
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.