How to Compare Strings in Python

closeup photo of computer keyboard

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.

For example:

name = "Jack"

print(name == "John")
print(name != "John")

Output:

False
True

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")

Output:

True

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).

UnicodeCharacterUnicodeCharacterUnicodeCharacterUnicodeCharacter
64@80P96`112p
65A81Q97a113q
66B82R98b114r
67C83S99c115s
68D84T100d116t
69E85U101e117u
70F86V102f118v
71G87W103g119w
72H88X104h120x
73I89Y105i121y
74J90Z106j122z
75K91[107k123{
76L92\108l124|
77M93]109m125}
78N94^110n126~
79O95_111o

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 ord() function.

Let’s see the Unicode values of characters a, b, c, and d:

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'

It is True.

This is because the ord() function gives 65 for 'A' and 97 for '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 “Alice" and "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 result:

True

The Python interpreter starts by comparing the first characters of the strings being compared. In case of "Alice" and "Bob" it starts by checking if 'A' is less than 'B':

ord('A') < ord('B') # Corresponds to checking if 65 < 66, which is True

As ord('A') returns the Unicode value of 65 and ord('B') 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 True.

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, "Axel" or "Alex", comes first in alphabetical order:

print("Axel" < "Alex")

The result:

False

This suggests that Alex comes before Axel, which is indeed the case.

This result is obtained under the hood as follows:

  1. 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.
  2. The second characters are are 'x' and 'l'. The unicode value for 'x' is 120 and 108 for 'l'. So 120 < 108 returns False. Thus comparison yields False.

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?

For instance:

print("Alex" < "Alexis")

Result:

True

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.

Conclusion

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.

Happy coding!

Further Reading

10+ Useful Python Tricks and Tips

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *