Saturday, 5 December 2026

Python ord() Function: Convert Characters into Unicode Values with Examples

 

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
48

Here:

  • A has Unicode value 65.

  • a has Unicode value 97.

  • 0 has 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
67

Example 2: Convert Lowercase Characters

print(ord('a'))
print(ord('b'))
print(ord('c'))

Output:

97
98
99

Example 3: Convert Numbers into Unicode Values

print(ord('1'))
print(ord('5'))
print(ord('9'))

Output:

49
53
57

Remember 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
37

Example 5: Unicode Characters and Emojis

The ord() function also supports Unicode characters beyond ASCII.

print(ord('₹'))
print(ord('€'))
print(ord('๐Ÿ˜Š'))

Output:

8377
8364
128522

This 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.

FunctionPurpose
ord()Character to Unicode integer
chr()Unicode integer to character

Example:

x = ord('A')
print(x)

y = chr(65)
print(y)

Output:

65
A

Both 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 110

This 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:

Hello

7. 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'))  # 5

These 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:

  1. Character encoding and decoding experiments.

  2. String manipulation and text processing.

  3. Understanding Unicode representation.

  4. Working with ASCII-based algorithms.

  5. Character comparison and transformation.

  6. 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.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (367) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (360) Bootcamp (21) C (78) C# (12) C++ (83) cloud (1) Course (98) Coursera (310) Cybersecurity (38) data (11) Data Analysis (48) Data Analytics (32) data management (17) Data Science (449) Data Strucures (22) Deep Learning (229) Django (17) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (14) flask (4) flutter (1) FPL (17) Gadgets (2) Generative AI (80) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (45) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (422) Meta (24) MICHIGAN (5) microsoft (14) Nvidia (8) Pandas (16) PHP (20) Projects (36) Python (1423) Python Bug (1) Python Coding Challenge (1323) Python Library (29) Python Mathematics (25) Python Mistakes (51) Python Pattern Challenge (53) Python Quiz (709) Python Tips (116) Python Turtle (3) Questions (3) R (72) RAG (6) React (7) Scripting (3) security (5) Selenium Webdriver (4) Software (21) SQL (57) Udemy (25) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)