Back to articles list Articles
7 minutes read

What Is the Python ord() Function? How Do You Use It?

Working with Unicode characters? You’ll need the Python ord() and chr() functions. Find out what each one does and why you should use them by reading this article.

In this article, we'll examine several examples of converting a character to its Unicode code using Python's ord() function. We’ll also review Unicode and Python's chr() function. So let's get started!

Unicode Basics

Computers, at their most fundamental, work with numbers. Under the hood, the characters and letters on a screen are encoded as a list of integers.

Before Unicode, there were hundreds of distinct character encoding techniques for allocating these numbers. These early character encodings were restricted in size and couldn't accommodate all of the world's languages. The Unicode Consortium was created to solve this problem. Unicode assigns each character a unique number, allowing a programmer to create strings with different characters coming from different languages, including emojis and other special characters.

Strings (aka text values) are a fundamental data type in programming, and you’ll likely come across many problems that can be solved using strings and Unicode. Therefore, we recommend checking out our interactive course on Working with Strings in Python. If you are a complete beginner, our Python Basics course will help you get started with Python.

What Does the ord() Function Do?

In Python, the ord() function returns the Unicode code for a character. This function takes a unit-length text as an argument and returns the Unicode equivalent of the specified parameter. When the argument is a Unicode object, Python's ord() method returns an integer corresponding to the Unicode code point of the character (or the value of the byte when the argument is an 8-bit string).

More on Unicode

Computer programs today must be able to handle a wide range of characters. Due to the globalization of applications, the same message may need to be displayed in various languages; for example, an application may need to output a message in Russian, English, French, and Japanese. Any of these languages can be used to create web content with many different characters, emojis, and other symbols. Python's string type represents characters using the Unicode standard, allowing Python programs to interact and deal with all these characters.

The Unicode standard attempts to list all of the characters used in human languages; it assigns each character a unique code number. Unicode specifications are regularly amended and updated to accommodate new languages and symbols.

A character is the smallest textual component: 'A,' 'B,' 'C,' etc. are all different characters. Characters in Unicode differ depending on the language or context in question. For example, the character for  the Roman numeral one (𐌠) looks the same as the uppercase letter 'I,'. But these are two distinct characters with quite different meanings.

The Unicode standard specifies how code points are used to represent characters. A code point value is an integer between 0 and 0x10FFFF (about 1.1 million values; the actual number currently assigned is less than that). So, for example, the code point U+265E denotes the character ♞ with the value 0x265e in the standard (9,822 in decimal). Likewise, the “\” character has the code point U+005C, with a value 0x05c in standard (92 in decimal).

Unicode has become a standard in many programming languages today, with many languages (including Python) using it to represent strings. Furthermore, it is used by all modern software providers and software developers as a sure way to deal with any input string.

How to Use the ord() Function in Python

The ord() function in Python is used to convert a single Unicode character to its integer equivalent. The function accepts any single string character and returns an integer. This method has the following syntax:

ord(x)

Here x represents any Unicode character.

Now, let's look at our first example using this method:

# Converting Unicode to Int Using ord()
character = 'd'

print(ord(character)) 

Output: 100

The print() function is used to output the value of our Unicode character. If the process seems unfamiliar, you can learn more about it in this article about the Python print function. It is worth noting that both single and double quotes will work; ‘d’ and “d” can be the argument to the ord() function.

# Converting Unicode to Int Using ord()
character = “d”

print(ord(character))

Output: 100

Let's also look at an example of the ord() function with a character not in the Latin alphabet – for instance, the dollar sign:

# Converting Unicode to Int Using ord()
character = '$'

print(ord(character)) 

Output: 36

If the string length is more than 1, a TypeError will be raised:

# Converting Unicode to Int Using ord()
character = 'data'

print(ord(character))

Output: TypeError: ord() expected a character, but string of length 4 found

We can see that using the ord() method with more than one character causes a TypeError. This occurs because the function only accepts a single character as input. To resolve this TypeError, we must loop over each character in the string. Since Python strings are iterable objects, we can easily iterate over these string values:

string = 'data'
for letter in string:
    print(ord(letter))

Output:
100
97
116
97

An important point to note is that the first 128 Unicode points are the same as ASCII values. This essentially means that the characters and their corresponding ASCII values will be equivalent to the same Unicode value.

For example:

character = ‘9’
print("ASCII value of 9 =", ord(character))

Output: ASCII value of 9 = 57

The ASCII value for the character ‘9’ is 57; as you can see, this is also equivalent to its Unicode point value.

If this seems overwhelming, don’t worry; we have you covered. As the old saying goes, practice makes perfect, so we recommend you check out this article to learn the best ways to practice Python. If you wish to go further and practice your Python skills, we highly recommend you check out LearnPython.com’s interactive course, Python Practice: Word Games. It will help you practice the ord() and chr() functions and get better at working with Unicode. If you wish to learn more about the course, check out this article on LearnPython’s Python Practice: Word Games.

The chr() Function

The Python chr() function turns an integer representation into its equivalent Unicode string character. In other words, it’s the opposite of the ord() function. This function takes any number between 0 and 1,114,111 to represent all Unicode characters.

The syntax of Python’s chr() function is as follows:

chr(i)

Where i is an integer, representing a Unicode code point of a character.

Example:

result = chr(102)

print(result) 

Output:  f

If a value outside of the range mentioned above is passed into the function, the function will raise a ValueError. Let’s see what this looks like:

result = chr(1114115)

print(result)

Output: ValueError: chr() arg not in range(0x110000)

This function also takes just one value; to convert many integers, we will have to iterate over all the values:

integers = [100, 97, 116, 97, 103, 121]

for integer in integers:
   print(chr(integer))

# Output:
d
a
t
a

We can take it further and turn this list of numbers into a Python string. This matches our text, 'data', which we transformed to Unicode in the previous example. We can employ the .join() method to do this:

integers = [100, 97, 116, 97, 103, 121]
result = list() # Initializing an empty list

for integer in integers:
   result.append(chr(integer)) # adding characters to a result array

Final = ‘’.join(result)

print(result) 

Output: data

Beyond chr() and ord() in Python Programming

In the programming industry, the Unicode standard has been revolutionary. Every character is assigned a numerical value, from letters to symbols to emojis. In addition, the standard made it much easier for computers to understand symbols, especially as more characters were added to the Internet.

We learned a great deal about Python’s ord() function in this tutorial. Each character has a unique Unicode value, and the ord() function can be employed to check for numerical values or special characters. We also learned about the chr() function, which is the opposite of the ord() function. And we saw several examples to help solidify both concepts.

Now all that you need to do is practice using chr() and ord() in Python. We highly recommend that you check out LearnPython’s Python’s Practice: Word Games course, and kick start your journey to become a Python master.