Python Func Ord
# Python ord() Function
The `ord()` function is a built-in Python function that serves as the inverse of `chr()`. It takes a single Unicode character as an argument and returns its corresponding Unicode code point as a decimal integer.
---
## Description
The `ord()` function accepts a string of length 1 (a single character) and returns an integer representing the Unicode code point of that character.
* For standard ASCII characters (such as English letters, numbers, and basic symbols), it returns their corresponding ASCII value (ranging from 0 to 127).
* For non-ASCII characters (such as Chinese characters, emojis, or accented letters), it returns their corresponding global Unicode code point.
* It is the exact counterpart to the `chr()` function, which converts an integer code point back into its character representation.
---
## Syntax
The syntax for the `ord()` function is as follows:
```python
ord(c)
```
### Parameters
* **`c`**: A string of length exactly 1 representing a single character.
### Return Value
* Returns a decimal **integer** representing the Unicode code point of the given character.
### Exceptions
* **`TypeError`**: Raised if the argument `c` is not a string, or if its length is not exactly 1 (e.g., `ord('ab')` or `ord('')`).
---
## Code Examples
### 1. Basic Usage with ASCII Characters
```python
# Get the ASCII values of lowercase letters
print(ord('a')) # Output: 97
print(ord('b')) # Output: 98
print(ord('c')) # Output: 99
# Get the ASCII values of uppercase letters and numbers
print(ord('A')) # Output: 65
print(ord('1')) # Output: 49
```
### 2. Working with Unicode Characters
Python 3 natively supports Unicode, allowing `ord()` to handle characters from various languages and symbol sets.
```python
# Chinese character
print(ord('δΈ')) # Output: 20013
# Euro currency symbol
print(ord('β¬')) # Output: 8364
# Emoji symbol
print(ord('π')) # Output: 128013
```
### 3. Round-Trip Conversion with `chr()`
Because `ord()` and `chr()` are inverse operations, passing the output of one into the other returns the original value.
```python
char_val = 'X'
# Convert character to code point, then back to character
code_point = ord(char_val)
restored_char = chr(code_point)
print(f"Original: {char_val}") # Output: Original: X
print(f"Code Point: {code_point}") # Output: Code Point: 88
print(f"Restored: {restored_char}") # Output: Restored: X
```
---
## Practical Applications
### 1. Character Offset Calculations
You can use `ord()` to perform alphabetical shifts, which is highly useful in cryptography algorithms like the Caesar Cipher.
```python
# Shift a character by 3 positions in the alphabet
original_char = 'A'
shifted_char = chr(ord(original_char) + 3)
print(shifted_char) # Output: D
```
### 2. Input Validation
You can check if a character falls within a specific range (e.g., checking if a character is a lowercase English letter).
```python
def is_lowercase_english(char):
if len(char) != 1:
return False
# 'a' is 97, 'z' is 122
return 97 <= ord(char) <= 122
print(is_lowercase_english('g')) # Output: True
print(is_lowercase_english('G')) # Output: False
```
---
## Considerations and Common Pitfalls
### Length Restriction
The input string must be **exactly one character** in length. Passing an empty string or a string with multiple characters will result in a `TypeError`.
```python
# This will raise a TypeError
try:
ord('abc')
except TypeError as e:
print(f"Error: {e}") # Output: Error: ord() expected a character, but string of length 3 found
```
YouTip