Python ord() Function: Convert Characters into Unicode Values with Examples
Introduction
Python provides several built-in functions that make programming easier. One useful function is ord(), which converts a single character into its corresponding integer Unicode code point.
Whether you are working with strings, character encoding, text processing, or Unicode data, understanding the ord() function can be helpful.
In this tutorial, we will explore Python's ord() function, its syntax, practical examples, and the difference between ord() and chr().
1. What is the ord() Function in Python?
The ord() function is a built-in Python function that returns the Unicode code point of a given single character as an integer.
For example:
print(ord('A'))
print(ord('a'))
print(ord('0'))Output:
65
97
48Here:
Ahas Unicode value 65.ahas Unicode value 97.0has Unicode value 48.
These values are also their ASCII values because ASCII is a subset of Unicode.
2. Syntax of ord()
ord(character)Parameter:
character: A string containing exactly one Unicode character.
Return Value:
An integer representing the Unicode code point of that character.
Important: If you pass an empty string or a string containing multiple characters, Python raises a TypeError.
3. Python ord() Function Examples
Example 1: Convert Uppercase Characters
print(ord('A'))
print(ord('B'))
print(ord('C'))Output:
65
66
67Example 2: Convert Lowercase Characters
print(ord('a'))
print(ord('b'))
print(ord('c'))Output:
97
98
99Example 3: Convert Numbers into Unicode Values
print(ord('1'))
print(ord('5'))
print(ord('9'))Output:
49
53
57Remember that ord('5') returns the Unicode value of the character '5', not the integer 5.
Example 4: Convert Special Characters
print(ord('@'))
print(ord('#'))
print(ord('$'))
print(ord('%'))Output:
64
35
36
37Example 5: Unicode Characters and Emojis
The ord() function also supports Unicode characters beyond ASCII.
print(ord('₹'))
print(ord('€'))
print(ord('๐'))Output:
8377
8364
128522This makes ord() useful when working with international characters, symbols, and emojis.
4. Difference Between ord() and chr()
Python provides another built-in function called chr().
While ord() converts a character into an integer, chr() performs the reverse operation.
| Function | Purpose |
|---|---|
ord() | Character to Unicode integer |
chr() | Unicode integer to character |
Example:
x = ord('A')
print(x)
y = chr(65)
print(y)Output:
65
ABoth functions are complementary.
5. Practical Example: Convert Every Character in a String
You can combine ord() with a Python loop to convert every character in a string into its Unicode code point.
text = "Python"
for char in text:
print(char, ord(char))Output:
P 80
y 121
t 116
h 104
o 111
n 110This is useful for understanding character representation and experimenting with text processing.
6. Practical Example: Convert Unicode Values Back into Characters
You can use chr() to convert a list of Unicode values into a string.
values = [72, 101, 108, 108, 111]
result = ""
for value in values:
result += chr(value)
print(result)Output:
Hello7. Common Mistakes When Using ord()
Mistake 1: Passing multiple characters
print(ord("Python"))This raises a TypeError because ord() accepts exactly one character.
Correct approach:
for char in "Python":
print(ord(char))Mistake 2: Confusing digits with integers
print(ord('5')) # 53
print(int('5')) # 5These functions perform different operations.
Mistake 3: Assuming every Unicode value is ASCII
ASCII covers only a limited range of characters. Unicode supports a much larger collection of characters, including international scripts and emojis.
8. Where Is ord() Used in Python?
Some practical applications include:
Character encoding and decoding experiments.
String manipulation and text processing.
Understanding Unicode representation.
Working with ASCII-based algorithms.
Character comparison and transformation.
Educational programming exercises.
Conclusion
The Python ord() function is a simple but powerful built-in function that converts a single character into its Unicode code point.
Remember these two functions:
ord()→ Character to Integer.chr()→ Integer to Character.
Understanding these functions will help you explore Python strings, Unicode, and character representation more effectively.
Keep practicing Python and explore more programming tutorials with CLCODING.
Learn Python for Free
Explore this beginner-friendly course:
Crash Course on Python by Google
CLCODING – Learn. Code. Build. Repeat.
%20Function_%20Convert%20Characters%20into%20Unicode%20Values%20with%20Examples.png)

0 Comments:
Post a Comment