Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is ord in Python

Understanding the ord Function in Python

When you're starting out with programming, you might come across situations where you need to deal with individual characters and their representations. Python, like many other programming languages, handles characters in a way that might not be immediately intuitive. This is where the ord function comes into play.

What Does ord Do?

Simply put, ord is a built-in function in Python that takes a character (a string of length one) and returns an integer representing its Unicode code point. This might sound a bit technical, but think of it as the unique number that identifies each character in the vast catalog of text symbols used by computers.

Unicode and Code Points: A Brief Overview

To understand ord, we need to take a quick look at Unicode. Unicode is a standard that assigns a unique number to every character, no matter the platform, program, or language. These unique numbers are called "code points." For example, the character 'A' has a code point of 65, and the character 'a' has a code point of 97.

Using ord in Python

Let's jump into some code examples to see ord in action. Open up your Python interpreter or create a new Python file, and type the following:

print(ord('A'))

When you run this code, it will output 65. This is because the Unicode code point for the uppercase letter 'A' is 65.

Now, try another character:

print(ord('a'))

This will output 97, the code point for the lowercase letter 'a'.

Why Do We Need ord?

You might be wondering why it's useful to know the code point of a character. There are several reasons:

  • Sorting: When you want to sort characters, knowing their code points helps you determine their order.
  • Conversion: Sometimes you need to convert characters to their numerical representation to perform certain operations.
  • Compatibility: When working with different systems or languages, using code points ensures that characters are understood correctly.

Working with ord and Characters

It's important to note that ord only works with single characters. If you try to use it with a string of more than one character, you'll get an error. For example:

# This will work
print(ord('z'))

# This will raise an error
print(ord('hello'))

The second line will give you a TypeError because 'hello' is a string of five characters, not a single character.

Beyond the Alphabet

ord isn't limited to just the letters of the alphabet. It can handle any Unicode character. This includes symbols, emojis, and characters from various writing systems around the world. For example:

# Symbol
print(ord('$'))

# Emoji
print(ord('😊'))

# Chinese character
print(ord('中'))

Each of these characters has a unique code point that ord can reveal to you.

Practical Uses of ord

Now that you know how ord works, let's look at some practical scenarios where you might use it.

Password Strength Checker

Imagine you're writing a simple program to check the strength of a password. You might use ord to ensure that the password includes a mix of characters with different code points, such as uppercase and lowercase letters, numbers, and symbols.

Data Encoding

In some advanced use cases, you might need to encode data in a certain way before transmitting it over a network or saving it to a file. You could use ord to convert characters to their numeric representations as part of this encoding process.

Implementing Ciphers

If you're interested in cryptography, you might use ord to implement simple ciphers. For instance, you could write a Caesar cipher, which shifts the letters in a message by a certain number of places in the alphabet. ord would help you get the code point of each letter, so you can then shift it appropriately.

Limitations of ord

While ord is useful, it does have limitations. It only works with single characters, so you can't use it directly on strings. Also, it's a one-way function—you give it a character, and it gives you a number. If you want to go from a number back to a character, you'll need to use the chr function, which is the inverse of ord.

Intuition and Analogy

Think of the ord function as a librarian who knows the exact location of every book in a vast library. Each book represents a character, and its location in the library is like the Unicode code point. When you ask the librarian (use ord) for the location of a specific book (a character), they give you a precise number (the code point) that tells you where to find it.

Conclusion: The Power of ord

In your journey as a budding programmer, understanding and using the ord function can be quite empowering. It's a bridge between the human-friendly characters we use every day and the numerical world that computers operate in. By mastering ord, you'll gain a deeper appreciation for how text is represented and manipulated in the digital realm.

As you continue coding, you'll find that ord is just one of the many tools in Python's rich toolbox that makes it such a versatile language. Whether you're sorting strings, encoding data, or exploring the basics of cryptography, ord provides a simple yet powerful way to interact with the building blocks of text. So go ahead, experiment with it, and watch as the characters on your screen transform into a world of numbers and back again!