ord() Python and chr() Python | Ordinal Value Character Conversions in Python

ord() Python and chr() Python | Ordinal Value Character Conversions in Python

Digital visualization of ord python focusing on converting characters to Unicode code points

Ever wondered how your computer displays emojis or distinguishes between ‘A’ and ‘a’? You’ve just stepped into the intriguing universe of Unicode characters. This blog post will delve into Python’s ord() and chr() functions, demonstrating how they can simplify your coding tasks.

So, how does Python, a leading programming language, manage Unicode characters? And as a Python coder, how can you manipulate these characters in your code? The answer lies in Python’s ord() and chr() functions.

In this post, we aim to provide a comprehensive understanding of Python’s ord() and chr() functions. We’ll discuss their purpose, operation, and practical usage in your code. Whether you’re an experienced Python developer or a coding novice, stay tuned as we navigate the universe of Unicode characters together!

TL;DR: What are Python’s ord() and chr() functions?

Python’s ord() and chr() functions are built-in functions for handling Unicode characters in Python. The ord() function converts a character into its Unicode point, while chr() does the opposite, converting a Unicode point back into its corresponding character.

These functions are essential for working with text in Python, especially when dealing with different languages, special symbols, or emojis. For more advanced usage, background, and tips, continue reading the article.


print(ord('A'))  # Output: 65
print(chr(65))  # Output: 'A'

Understanding Python’s ord() Function

If you’d like to understand Unicode before you dive into the ord() and chr() functions, skip ahead to Unicode: Revolutionizing Character Encoding

One of Python’s key functions for managing Unicode characters is the ord() function. In this section, we’ll delve into this function and its application in your code.

The Role of the ord() Function

Python’s ord() function is a built-in function that accepts a string containing a single Unicode character and returns an integer representing the Unicode point of that character. For instance, ord('A') would return 65, the Unicode point for the character ‘A’.

print(ord('A'))  # Output: 65

Translating Characters to Unicode with ord()

The ord() function proves incredibly useful when you need to convert characters to their corresponding Unicode points. This comes in handy in several situations, such as comparing characters, implementing encryption algorithms, or working with data in diverse languages.

print(ord('€'))  # Output: 8364
print(ord('字'))  # Output: 23383

The Relationship Between ASCII and Unicode

Although the Unicode standard can represent over a million unique characters, the first 128 Unicode points align with the ASCII standard. This means that for characters in the ASCII range (0-127), the ord() function will return the ASCII value of the character.

print(ord('Z'))  # Output: 90

The ord() Function and Single Characters

As previously mentioned, the ord() function can only process a single character. If you need to convert a string with multiple characters to Unicode, you would need to iterate over the string and call ord() for each character.

s = 'Hello'
for char in s:
# Output:
# 72
# 101
# 108
# 108
# 111

Next, we’ll explore Python’s chr() function, the counterpart of ord(), which converts Unicode points back to characters.

Handling Errors in the ord() Function

It’s crucial to note that the ord() function expects a single character. If you pass a string with more than one character or an empty string, Python will raise a TypeError.

print(ord('AB'))  # Output: TypeError: ord() expected a character, but string of length 2 found
print(ord(''))    # Output: TypeError: ord() expected a character, but string of length 0 found

Decoding Unicode with Python’s chr() Function

Having explored Python’s ord() function, it’s time to shift our focus to its counterpart: the chr() function. This function performs the inverse operation of ord(): it accepts a Unicode point (an integer) and returns the corresponding character.

The Functionality of chr()

In Python, the chr() function is a built-in function that accepts an integer as its argument and returns a string representing a character at that Unicode point. For example, chr(65) would return ‘A’, the character for the Unicode point 65.

print(chr(65))  # Output: 'A'

Translating Unicode to Characters with chr()

The chr() function proves extremely useful when you need to convert Unicode points to their corresponding characters. This can be beneficial in various situations, such as decoding encrypted data or displaying characters from different languages.

print(chr(8364))  # Output: '€'
print(chr(23383)) # Output: '字'

The Scope of the chr() Function

The chr() function can convert any valid Unicode point to a character. This means it can handle any integer in the range of 0 through 1,114,111, covering all Unicode characters, including language scripts, special symbols, and emojis.

print(chr(128512))  # Output: ''

Error Handling in chr()

It’s crucial to understand that the chr() function expects an integer as its argument. If you pass a non-integer or a negative integer, Python will raise a TypeError or ValueError respectively. Furthermore, the integer must be a valid Unicode point (0 through 1,114,111).

print(chr('65'))  # Output: TypeError: an integer is required (got type str)
print(chr(-1))    # Output: ValueError: chr() arg not in range(0x110000)

Converting a List of Numbers into a String

A unique application of the chr() function is its ability to convert a list of Unicode points into a Python string. This can be achieved by iterating over the list, calling chr() for each number, and then joining the results.

numbers = [72, 101, 108, 108, 111]
string = ''.join(chr(num) for num in numbers)
print(string)  # Output: 'Hello'

Unicode: Revolutionizing Character Encoding

Now that we understand Python’s ord() and chr() functions, let’s explore the universe of Unicode characters. But what is Unicode, and why is it crucial in today’s computing world?

The Language of Computers: Binary

At its core, a computer is a machine that understands binary – a system of 1s and 0s. Every piece of information processed by a computer, including text, is ultimately converted into binary. This conversion is done through character encoding, which assigns a unique number to each character.

The Chaos Before Unicode

In the early days of computing, multiple character encoding systems existed, each with its unique set of assigned numbers. ASCII (American Standard Code for Information Interchange) was widely used. However, ASCII could only represent 128 characters, sufficient for English but inadequate for other languages. This limitation led to various extended ASCII sets, creating a chaotic situation where the same number could represent different characters in different systems.

Encoding SystemNumber of Characters
Extended ASCII256

Unicode: A Universal Solution

Recognizing the need for a universal character encoding system, the Unicode Consortium was formed. The consortium introduced the Unicode Standard, a character encoding system capable of accommodating a vast array of characters from different languages, special symbols, and even emojis.

Strings and Unicode in Programming

In programming, strings (a sequence of characters) are fundamental. They represent and manipulate text. With the advent of the Unicode Standard, handling strings became more versatile and inclusive. Now, a single string can include characters from different languages, special symbols, and emojis, making our programs truly global.

Example of a string with characters from different languages:

s = 'Hello, 你好, Bonjour, Hola, こんにちは'
# Output: Hello, 你好, Bonjour, Hola, こんにちは

The Evolution of Character Encoding

The transition from ASCII to Unicode was a gradual evolution, not a sudden leap. As our digital world expanded, so did our need for a more comprehensive character encoding system. From ASCII to Extended ASCII, to ISO 8859, and finally to Unicode, each step was a response to the increasing complexity and diversity of our digital communication needs.

Encoding SystemNumber of Characters
Extended ASCII256
ISO 8859191

The Impact of Unicode

The introduction of Unicode was a game-changer. It brought order to the chaos of multiple character encoding systems. With its ability to represent over a million unique characters, Unicode supports almost all language scripts, special symbols, and a wide range of emojis. This has not only made programming more inclusive but also opened up new possibilities for digital communication.

In the following sections, we’ll explore how Python harnesses the power of Unicode through its ord() and chr() functions.

Further Resources for Python Functions

To aid you in expanding your knowledge on Python functions, we’ve compiled a selection of informative resources for your perusal:

These additional resources will aid in deepening your understanding of Python functions, making you even more proficient in your Python development journey.

Conclusion: Exploring Unicode with Python

As we’ve journeyed through the world of Unicode and Python’s ord() and chr() functions, we’ve seen the profound impact they’ve had on the realm of programming. These tools have unlocked a global digital communication experience, enabling our applications to support a vast array of languages, symbols, and emojis.

The ord() function, with its ability to convert a character into its Unicode point, and the chr() function, performing the inverse operation, are indispensable tools in any Python programmer’s toolkit. They offer a simple yet effective way to handle text in diverse languages, work with unique symbols, and even create your own emojis!

To learn more about Python, visit this page.

So, the next time you’re working with text in your Python code, remember the power of Unicode and the simplicity of Python’s ord() and chr() functions. Embrace the Unicode universe and experience the ease Python brings to your coding journey!