Often Python developers need to compare strings in Python for different purposes. You may need to perform string comparison to check if they are equal or not, or when you need to sort them, or to check if one string is a substring of another, and more. There are several ways to easily compare strings in Python. In this article, we will learn how to compare strings in Python. But before we dive into the topic, it is important to know the different types of string comparisons that can be in Python.
Types of String comparison in Python
There are different types of string comparison available in Python. Depending on the kind of comparison you want to perform, you need to use the appropriate operator or function. Let us look at them one by one.
- Lexicographic comparison – In this case, we purely compare the values of the strings. We check whether they have the same sequence of characters, or if one comes before the other alphabetically. This kind of comparison is done using the Unicode values of the strings
- Memory location comparison – Here we check if the strings refer to the same memory location or not. We don’t compare their values.
- Case insensitive – If you try to compare strings that have the same sequence of characters but different letter case, then you will get a mismatch. In such cases, you need to do a case insensitive match by converting them into same lower/upper case.
- Substring check – In this case, we simply check if one string is a substring of the other. This is required if there is a partial match, or you just want to check the presence of one string ianother.
Now that you have an idea of the different ways to compare strings, let us look at the operators and functions available to do so.
How to Compare Strings in Python
Here are the different ways to compare strings in Python.
1. Using Comparison Operators
In this solution, we compare strings using operators ==, !=, <, >, <= and >=. In each case, the Unicode values of strings are compared. These are generally used to compare all sorts of data such as numbers, lists, and so on. You can also use them for strings. Here is how the different operators work
- == – check if two strings are equal
- != – check if two strings are unequal
- < – checks if the left string comes alphabetically before the right string
- > – checks if the right string comes alphabetically after the right string
- <= – check if the left string is equal or alphabetically before the right string
- >= – check if the right string is equal or alphabetically after the right string
Here are the different examples to demonstrate their application
a = 'apple'
b = 'banana'
c = 'apple'
print(a==c) # True
print(a!=c) # False
print(a==b) # False
print(a<b) # True
print(b<=c) # False
print(c>b) # False
print(c>=a) # True
2. Using is and is not operators
Sometimes, you may need to check if two strings (variables or literals) are stored at the same location. In this case, you are not interested in comparing their values but only their location to see if they are nothing but the same memory reference. For this purpose, you can use ‘is’ and ‘is not’ operators. The ‘is’ operator returns true if the strings refer to the same memory location, else it returns false.
a = 'apple'
b = 'banana'
c = a
print(a is c) # True
print(a is b) # False
The ‘is not’ operator returns true if both strings refer to the same memory location, else it returns false.
print(a is not c) # False
print(a is not b) # True
3. Case insensitive comparison
Sometimes the strings you are comparing, may be in different cases. In such instances, even if they contain the same sequence of characters, still you will get a mismatch. Here is an example to show it.
a = 'apple'
b = 'Apple'
print(a == b) # False
Therefore, you need to convert both the strings to the same case, upper or lower, before comparison. You can use lower() function to convert all characters of a string to the lowercase. You can use upper() function to convert all characters of string to uppercase.
a = 'apple'
b = 'Apple'
print(a.lower()) # output is apple
print(a.upper()) # output is APPLE
print(a.lower() == b.lower()) # True
print(a.upper() == b.upper()) # True
4. Substring comparison
In this situation, we check if one string is a part of the other. We do not check if they are equal or not. This is useful if you do not want to check for equality but mere presence of one string in the other. For this purpose, we use ‘in’ and ‘not in’ operators. The ‘in’ operator returns True, if one string is present in another, else it returns False. The ‘not in’ operator returns True, if one string is not present in the other, else it returns True.
a = 'apple'
b = 'apple pie'
c = 'banana'
print(a in b) # True
print(b in a) # False
print(a in c) # False
print(a not in c) # True
As you can see, the order of strings is important while using in/not in operators. If you do not maintain the correct order, you may get unexpected result. When we checked if string a is present in b, we got True. When we checked if b is present in a, we got False.
You can also use this solution to check if a string is present in a list of strings.
a = ['apple','banana','cherry']
b = 'apple'
c = 'dates'
print(b in a) # True
print(c in a) # False
Sometimes, you may need to also check if one string starts or ends with another string. For this purpose, you can use startswith() and endswith() functions. You can directly call them from any string variable or literal. Here are their syntaxes.
string.startswith(substring)
string.endswith(substring)
You need to call them on the original string and pass the substring as argument.
Here are a couple of examples to demonstrate their use.
a = 'apple'
b = 'apple pie'
c = 'banana'
d = 'ripe banana'
print(b.startswith(a)) # True
print(d.endswith(c)) # True
Conclusion
In this article, we have learnt several simple ways to easily compare strings in Python. We have learnt the different types of string comparison available in Python. We have also learnt the different operators and functions available for each of these types. We learnt how to perform lexicographic checks that compare only the character sequence of strings. We also learnt how to check if the strings refer to the same memory location. Then we also learnt how to compare strings if they are different letter case. Lastly, we learnt how to check if one string is a substring of another. You can use any of these solutions as per your requirement.
Also read:
How to Remove Characters from Python String
What are f-strings in Python? How to use f-strings?
How to Work with ZIP Files in Python

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.