- Understanding Python’s ord() Method
Just now·3 min read
A quick tutorial on Python’s ord() function with examples.
Photo by Brett Jordan on Unsplash
In Python, the ord(c) function returns an integer representing the Unicode of the specified character c.
Example of the function below:
How does ord('a') return integer 97?
The Unicode representation of character “a” is 97. I provided the ASCII table shown below as a quick reference since the ASCII table is a subset of the Unicode.
I became interested in Python’s ord() function during my technical interview preparation when I stumbled upon code written like this:
According to the ASCII table, ord('c') and ord('a') equals to 99 and 97 respectively. Next, 99-97 = 2. The result is important because 2 is the index of c in the English alphabet (starting at the 0th index).
0, 1, 2
a, b, c
Another example, ord('k') — ord('a') is equivalent to 107-97 = 10. The result 10 is also the index of k in the alphabet.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
a, b, c, d, e, f, g, h, i, j, k
Subtracting a character by ord('a') returns the index of that character in the alphabet.
For now, let’s focus on lowercase letters, we have the entire alphabet from “a” to “z” with corresponding values of 97 to 122. Including 97 all the way up to 122 is 26 letters, which is also the same number of letters in the English alphabet.
How is this useful?
I constructed an array of length 26 filled with integers which are all initially assigned with a value of 0. The newly formed array represents the entire alphabet. Next, we iterate through a given string and update the array at the appropriate indices. Let’s see what the array would look like with the given string ‘hello’.
The resulting array shows the number of times each letter appears in our given string. There is one occurrence of h, one occurrence of e, two occurrences of l, and one occurrence of o.
This technique can be used to solve problems such as getting the character counts in a given string or checking if two strings are permutations of each other.
Example 1: Get the character counts in the given string.
The output of the above code shows which letters appear in our input string and the number of times they appear. There is one occurrence of c, one occurrence of e, one occurrence of i, one occurrence of k, and two occurrences of o.
Now that we understand how the ord() function and char subtraction works, we can also use this function to check if two strings are permutations of each other. A permutation is the rearrangement of a word or phrase that maintains the same characters as the original word.
Example 2: Check whether two words are permutations of each other.
Given the two strings, “chocolate” and “ocohtlaec”, check whether the given strings are permutations of each other.
We constructed an array for each given string of length 26 filled with 0 initially. Then, we iterate through each given strings and update the number of times each letter appears.
The resulting arrays from the above code are considered equal to each other. This means that both strings contain the same letters and its occurrences, indicating that the two words are permutations of each other.
The Python ord() function is useful for representing characters as integers. As a result, the ord() function makes it simple to retrieve which letters and the number of times they appear within a given string. The function also introduces ways to make comparisons to another given string as shown in Example 2. Now it’s time for me to get back to studying, but in the meantime, I’d like to hear what problems you’ve been able to solve using the ord() function.
- Date of publication:
- Fri, 01/14/2022 - 04:32
Click on the link - it will be copied to clipboard