Introduction: Python ord() Function
When working with Python, there are situations where you need to find the Unicode value of a character. Whether you’re processing text, comparing characters, or working with character encoding, manually looking up Unicode values can quickly become inconvenient.
Without a built-in solution, you would need to rely on external Unicode reference tables or write additional logic to determine the numeric value of a character.
A simple and efficient solution to these situations is the Python ord() Function.
What it is: The ord() function is a built-in Python function that returns the Unicode code point of a single character. It is commonly used when working with character encoding, text processing, and Unicode-based operations.
Take a look at a quick example to see how it works.
You can also explore its real-world use cases to learn where it is commonly used.
Now let’s explore its syntax, parameters, return value, and practical examples.
💡 Tip: The ord() function is one of many useful Python built-in functions. Explore the complete Python Built-in Functions Learning Guide to discover more functions with practical examples.
Syntax, Parameters, Return Value and Examples: Python ord() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python ord() Function.
Syntax
ord(c)
Parameters
| Parameter | Description |
|---|---|
c |
A single Unicode character whose numeric code point is required. |
Return Value
| Return Value | Description |
|---|---|
int |
Returns the Unicode code point of the specified character. |
Quick Example
The following example returns the Unicode value of a character.
result = ord("A")
print(result)
# Output:
65
The ord() function converts the character 'A' into its corresponding Unicode code point, which is 65.
How the Python ord() Function Works
- The
ord()function accepts a single character as its argument. - The character must be a Unicode character represented as a string of length one.
- It finds the Unicode code point assigned to that character.
- The Unicode value is returned as an integer.
- It works with letters, digits, symbols, emojis, and other Unicode characters.
Examples: Python ord() Function
The following examples show how the Python ord() Function works in different programming scenarios.
Example 1: Finding the Unicode Value of a Letter
result = ord("a")
print(result)
# Output:
97
Explanation: The lowercase letter 'a' has the Unicode value 97. The ord() function returns this numeric code point.
Example 2: Getting Unicode Values for Multiple Characters
for letter in "ABC":
print(ord(letter), end=" ")
# Output:
65 66 67
Explanation: Instead of checking each character individually, the loop processes every letter and prints its corresponding Unicode value.
Example 3: Processing User Input
character = input("Enter a character: ")
print(ord(character))
# Sample Output:
Enter a character: Z
90
Explanation: After the user enters a character, ord() converts it into its Unicode code point and displays the result.
Example 4: Finding the Unicode Value of a Symbol
print(ord("$"))
# Output:
36
Explanation: The ord() function works with symbols as well as letters. Here, it returns the Unicode value assigned to the dollar sign.
Example 5: Finding the Unicode Value of an Emoji
print(ord("😀"))
# Output:
128512
Explanation: Unicode is not limited to letters and symbols. Emojis also have unique Unicode code points, which ord() can return.
Example 6: Handling Invalid Input
try:
print(ord("AB"))
except TypeError as e:
print(e)
# Output:
ord() expected a character, but string of length 2 found
Explanation: The ord() function accepts only one character at a time. Since the string contains two characters, Python raises a TypeError. The exception is handled using a try-except block so the program continues to run.
Example 7: Comparing Characters Using Unicode Values
print(ord("A") < ord("B"))
# Output:
True
Explanation: Each character has a numeric Unicode value. Comparing these values makes it easy to determine their relative order.
Use Cases: When to use the ord() Function
Below are some common situations where the Python ord() Function becomes useful:
- Finding the Unicode value of a character.
- Working with character encoding.
- Comparing characters using Unicode values.
- Processing letters, symbols, and emojis.
- Implementing text-processing algorithms.
- Converting characters into numeric values for further calculations.
Key Takeaways: ord() Function
Before wrapping up, here are the key points to remember about the Python ord() Function:
- The
ord()function returns the Unicode code point of a single character. - It accepts one Unicode character as its argument.
- It returns the corresponding Unicode value as an integer.
- It works with letters, digits, symbols, and emojis.
- Passing more than one character raises a
TypeError. - It provides a simple way to convert characters into Unicode values.
In short, the Python ord() Function offers a simple and reliable way to convert characters into their Unicode values, making character processing and Unicode-related programming much easier.