How to compare strings in Python

There are different comparison operators in Python which we can use to compare different object types. The comparison operators (<, <=, >, >=, ==, and !=) work with numbers, strings, lists, and other collection objects and return True if the condition holds. For collection objects, these operators compare the number of elements and the equivalence operator == b returns True if each collection object is structurally equivalent, and the value of each element is identical.

Comparison of objects of the same type depends on the type in Python:

  • Numbers are compared arithmetically.
  • Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord()) of their characters. Unicode and 8-bit strings are fully interoperable in this behavior.
  • Tuples and lists are compared lexicographically using comparison of corresponding items.
  • Mappings (dictionaries) are compared through lexicographic comparison of their sorted (key, value) lists
  • Most other types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program.

 

Comparison operators for strings in Python

NameOperatorExampleMeaning
is greater than>x > yReturns True if left operand is greater than the right operand
is less than<x < yReturns True is left operand is less than the right operand
is equal to==x == yReturns True if both operands are equal
is not equal to!=x != yReturns True if operands are not equal
is greater than or equal to>=x >= yReturns True if the left operand is greater than or equal to right operand
is less than or equal to<=x <= yReturns True if the left operand is less than or equal to the right operand

 

Basic string comparison using is equal to operator

First we will use comparison operator to check if the strings in different variables are equal in Python scripts. I have created a sample script with two variables having the same set of string characters

#!/usr/bin/env python3

var1 = 'abc'
var2 = 'abc'

# Check if both variables are equal
print('var1 is Equal to var2:', var1 == var2)

# Check if both variables are not equal
print('var1 is Not Equal to var2:', var1 != var2)

The output from this script would be a boolean expression based on the comparison result

# python3 /tmp/comp_strings.py
var1 is Equal to var2: True
var1 is Not Equal to var2: False

 

Case insensitive comparison with upper() or lower()

To check if strings are equal and ignore the case then we can use either upper() or lower() function to transform the string value and then perform the comparison. But this would expect the strings to be in ASCII format. upper() converts the entire string into uppercase letters, and lower() into lowercase letters, respectively.

In this example I am converting the string value to uppercase and then comparing them.

#!/usr/bin/env python3

var1 = 'abc'
var2 = 'ABC'

# Check if both variables are equal using upper()
print('var1 with upper(): ', var1.upper())
print('var2 with upper(): ', var2.upper())
print('var1 is Equal to var2:', var1.upper() == var2.upper())

Output from this script:

# python3 /tmp/comp_strings.py
var1 with upper():  ABC
var2 with upper():  ABC
var1 is Equal to var2: True

 

Case insensitive comparison with casefold()

Casefolding is similar to lower casing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase, lower() would do nothing to 'ß'; casefold() converts it to "ss".

#!/usr/bin/env python3

var1 = 'abc'
var2 = 'ABC'

# Check if both variables are equal using casefold()
print('var1 with casefold(): ', var1.casefold())
print('var2 with casefold(): ', var2.casefold())
print('var1 is Not Equal to var2:', var1.casefold() == var2.casefold())

The output from this script:

# python3 /tmp/comp_strings.py
var1 is Equal to var2: True
var1 with casefold():  abc
var2 with casefold():  abc

 

String comparison using sorted()

To sort the strings before performing the comparison we can use sorted() in-built function that builds a new sorted list from an iterable.

#!/usr/bin/env python3

var1 = 'abc def ghi'
var2 = 'ghi abc def'

# Sort the strings and remove whitespace
sort_var1 = ''.join(sorted(var1)).strip()
sort_var2 = ''.join(sorted(var2)).strip()

# Check if both variables are equal
print('var1 with sort_var1: ', sort_var1)
print('var2 with sort_var2: ', sort_var2)
print('var1 is Equal to var2:', sort_var1 == sort_var2)

 

Compare strings using collections.Counter()

If you want to know if both the strings have the same set of characters and they occur same number of times, we can use collections.Counter() class. A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts.

#!/usr/bin/env python3

from collections import Counter

var1 = 'abc def ghi'
var2 = 'ghi abc def'

# Sort the strings and remove whitespace
new_var1 = Counter(var1)
new_var2 = Counter(var2)

# Check if both variables are equal
print('new_var1: ', new_var1)
print('new_var2: ', new_var2)
print('var1 is Equal to var2:', new_var1 == new_var2)

The output from this script:

# python3 /tmp/comp_strings.py
new_var1:  Counter({' ': 2, 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1, 'i': 1})
new_var2:  Counter({' ': 2, 'g': 1, 'h': 1, 'i': 1, 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1})
var1 is Equal to var2: True

 

Greater than (>) or lesser than (<) operators

The strings in Python are compared lexicographically using the numeric equivalents which can be collected using built-in function ord() of individual characters of the string. The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

To get the numeric value of individual character

#!/usr/bin/env python3

var1 = 'a'

print('Numerical Value:', ord(var1))

Output from this script:

# python3 /tmp/comp_strings.py
Numerical Value: 97

Here the value of 'a' is 97, similarly you can collect these values for other alphabets. Similarly we will compare longer strings

#!/usr/bin/env python3

var1 = 'abc'
var2 = 'abd'

# Get the ord(0 values for individual char in both variable
ord_var1 = [ ord(x) for x in var1 ]
print('var1:', ord_var1)

ord_var2 = [ ord(x) for x in var2 ]
print('var2:', ord_var2)

# perform comparison
print('var1 is greater than var2:', var1 > var2)
print('var1 is lesser than var2:', var1 < var2)
print('var1 is greater than or equal to var2:', var1 >= var2)
print('var1 is lesser than or equal to var2:', var1 <= var2)

Output from this script:

# python3 /tmp/comp_strings.py
var1: [97, 98, 99]
var2: [97, 98, 100]
var1 is greater than var2: False
var1 is lesser than var2: True
var1 is greater than or equal to var2: False
var1 is lesser than or equal to var2: True

 

Conclusion

In this tutorial we learned about different methods using which we can performing Python string comparison using various operator and other functions. Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false). Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics.

Lastly I hope this tutorial to lean about different comparison operators for Python strings in Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References

I have used below external references for this tutorial guide
Python Comparison Operatos
collections.Counter docs.python.org
str.casefold docs.python.org

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.