Python string comparison is possible using the comparison operators:
==, !=, <, >, <=, >=:
"Alice" == "Bob" # False "Alice" != "Bob" # True "Alice" < "Bob" # True "Alice" > "Bob" # False "Alice" <= "Bob" # True "Alice" >= "Bob" # False
The Art of String Comparison in Python
If you have a Python program that deals with strings, it is common you want to compare them.
Python comes with a list of built-in comparison methods:
==, !=, <, >, <=, >=. Commonly you see these being used with numbers. But you can use these with strings as well. As it turns out, comparing strings actually means comparing numbers under the hood.
Before going into further details, let’s see how you can compare strings in Python using the comparison operators.
Comparing Strings with == and !=
Comparing strings with equal to and not equal to operators is easy to understand. These check if the string is equal to another string.
name = "Jack" print(name == "John") print(name != "John")
Comparing Strings with <, >, <=, and >=
To compare strings alphabetically, you can use the operators
For instance, let’s compare the names “Alice” and “Bob”. This comparison corresponds to checking if Alice is before Bob in alphabetical order. The answer is true. But let’s verify it using Python:
print("Alice" < "Bob")
Now you have the tools for comparing strings in Python. Next, let’s understand how the string comparison works behind the scenes.
String Unicodes in Python
In reality, comparing strings means comparing integers under the hood.
To understand how this is possible, you first need to understand the concept of Unicode.
Python string uses the Unicode Standard for representing its characters. This means each character has a unique integer code assigned to it. This code is used when making comparisons.
Here is the Unicode table for English characters (also known as the ASCII values).
When your Python program compares strings, it compares the Unicode values of the characters one by one.
If you want to check the Unicode of a character, you do not have to use this table. Instead, you can use the built-in
Let’s see the Unicode values of characters
ord('a') # returns 97 ord('b') # returns 98 ord('c') # returns 99 ord('d') # returns 100
Now, let’s check the Unicode values for the capitalized versions of these:
ord('A') # returns 65 ord('B') # returns 66 ord('C') # returns 67 ord('D') # returns 68
The Unicode values for capitals are different. This is important—Python is case-sensitive when it comes to characters.
Can you now guess what is the result of this comparison?
'A' < 'a'
This is because the
ord() function gives
'a'. And as you know,
65 < 97.
How Python String Comparison Works Under the Hood
When you compare strings in Python the strings are compared character by character using the Unicode values.Let’s demonstrate the process with examples.
Example 1—Which String Comes First in Alphabetic Order
Let’s compare two names “
"Bob" to see if
"Alice" is less than
"Bob". In other words, we want to figure out if Alice is before Bob in alphabetical order. The answer is yes. But let’s verify this using Python and show how Python comes with this conclusion under the hood:
print("Alice" < "Bob")
The Python interpreter starts by comparing the first characters of the strings being compared. In case of
"Bob" it starts by checking if
'A' is less than
ord('A') < ord('B') # Corresponds to checking if 65 < 66, which is True
ord('A') returns the Unicode value of
66, the comparison evaluates to
True. This means the Python interpreter does not need to continue any further. Based on the very first letter it is able to determine that “Bob is greater than Alice” because ‘B’ is greater than ‘A’ in Unicode. So it returns
Example 2—How to Compare Strings with Equal First Letters
What if the first letters are equal when comparing two strings? No problem, Python then compares the second letters and so on.
When faced with this situation in the real-life, you would jump to the next character until they are different. Only then could you make a comparison to order them correctly. This also is exactly what Python does.
For instance, let’s check which name,
"Alex", comes first in alphabetical order:
print("Axel" < "Alex")
This suggests that Alex comes before Axel, which is indeed the case.
This result is obtained under the hood as follows:
- The first letters are compared. Both are
'A', so there is a “tie”. There is no way to tell which string is greater based on the first characters because they are the same. So the comparison continues from the next characters.
- The second characters are are
'l'. The unicode value for
120 < 108returns
False. Thus comparison yields
Example 3—How to Compare Equal Strings Where the Other One Has Extra Characters
What if the strings are otherwise equal, but there are additional characters in another one?
print("Alex" < "Alexis")
In this case, the Python interpreter simply treats the longer string as a greater one.
Now you understand how the string comparison works under the hood in Python.
Comparing strings is an important feature in Python.
Python’s built-in comparison operators can be used in string comparison. These built-in operators are:
- equal to (
- not equal to (
- greater than (
- less than (
- less than or equal to (
- greater than or equal to (
Under the hood, there is no such thing as string comparison. Instead, the numeric codes of the characters are compared with one another.
Thanks for reading. I hope you find it useful.