SoFunction
Updated on 2025-03-02

Example of implementation of Python built-in function ord

In Python programming,ord()Functions are a very practical built-in function that returns a Unicode code point for a character, i.e. the numerical value of the character in the Unicode encoding table. This feature is especially important when processing text data and internationalizing applications.

Functional Function

The main function of the ord() function is to convert a single character into its corresponding Unicode code point. Unicode is a character encoding standard that allows the use of the same code points to represent characters around the world.

Function Syntax

ord(c)
  • c: Must be a string of length 1, that is, a character.

Return value

The function returns an integer representing the position of the characters in the Unicode table.

Sample code

Let's take a look at it with some simple examplesord()How functions work:

# Get Unicode code points for charactersprint(ord('a'))  # Output: 97print(ord('middle'))  # Output: 20013print(ord('😊'))  # Output: 128522
# Use ord() for character comparisonchar1 = 'a'
char2 = 'b'
print(ord(char1) < ord(char2))  # Output: True
# Convert Unicode code points back to charactersprint(chr(ord('a')))  # Output: 'a'

In the above example, we can seeord()How a function converts different characters into its Unicode code points. We also show how to useord()Functions perform character comparisons and how to convert code points back to characters.

Things to note

  • The ord() function can only be used for a single character. If the incoming string length exceeds 1, or the incoming string is not a string type, a TypeError will be raised.
  • For non-ASCII characters, the ord() function is also applicable and can return its corresponding Unicode code bit.

This is the end of this article about the implementation example of Python built-in function ord(). For more related contents of Python built-in function ord(), please search for my previous article or continue browsing the following related articles. I hope everyone will support me in the future!