Various methods for Python String Comparison

In this short tutorial, let us look at the various string comparison methods in python. We also look at the various edge cases, limitations and caveats.

Table of Contents

  1. Python String Comparison
  2. String Comparison Operators
  3. Limitations and Caveats
  4. Other Related Concepts

Python String Comparison

Python string comparison is very common and is often used as a condition for loops. Python also comes with a few handy inbuilt operators to facilitate this. However before we dive into the methods, we have one important concept to touch upon.

All the data in your program are represented as objects, and every object has the following 3 properties. **Identity** **(Id)** - One may think of identity as the address of the memory in with the data is stored. The other two properties are **Type** and **Value.** Type is the data type of the object and Value is the value that the object stores. Python tries to save memory by re-using Id for objects with the same value an example of this here, this makes python string comparison much faster and easier. Also, please be vary of these terms as each operator uses a different comparison method.

String Comparison Operators

Out of the multiple methods that could be used to compare strings in python, I've explained two of the most commonly used, methods below. Note that all these methods return a boolean true or false.

"==" and "!="

The == and != are commonly used relation operation in python string comparison. Both these operators compare the Unicode values of all the characters in the string one by one and then return a value. In case you aren't familiar with Unicode values, it essentially means that strings get converted into a Unicode, this helps maintain a uniform code irrespective of the language the programmer uses. You can read more about this here. So based on these values you each character of a string is compared.

Using "=="
The "==" is a python string comparison method that is used to check if the value of both the operands is equal. And this is the most common method used to check equality.

s1 = 'flexiple!'
print(id(s1))
#Output = 2621679855024
s2 = 'flexiple!'
print(id(s2))
#Output = 2621679855024
s3 = 'flexiple'
print(id(31))
#Output = 140735453670112

print(s1==s2)
#output = True
print(s2==s3)
#output = False

The operator returns True and False respectively. Also notice how the Id of s1 and s2 is identical, however, bear in mind that the Id function will return a different number for you.

Using "!="
The != is another python string comparison operator that is used to check if the values of the operands are not equal. Basically, the inverse of == and the code makes it pretty straightforward to understand.

s1 = 'flexiple!'

s2 = 'flexiple!'

s3 = 'flexiple'

print(s1!=s2)
#Output = False
print(s2!=s3)
#Output = True


"is" and "not is"


The is and not is operators are also quite similar to == and != respectively. However, is and is not compares to the Identity (id) of the objects and returns true only if they share the same identity. One could argue that the identity of the object remains the same, but this is not the case when working with immutables. When the object is given another value the memory allocated changes giving it a new id. This is something you need to keep in mind while using is and is not.

Using "is":
s1 = 'flexiple!'

s2 = 'flexiple!'

s3 = 'flexiple'

print(s1 is s2)
#output = True
print(s2 is s3)
#output = False


Using "is not":
table - The values could either be a dictionary or a mapping table that describes what values should be replaced.

s1 = 'flexiple!'

s2 = 'flexiple!'

s3 = 'flexiple'

print(s1 is not s2)
#output = False
print(s2 is not s3)
#output = True


Now let's look at how the Identity (id) changes when the value is changing.

s1 = 'flexiple!'
print(id(s1))
#Output = 2621679855024
s2 = 'flexiple!'
print(id(s2))
#Output = 2621679855024

#Now let us update s1
s1 = 'flexi'
print(id(s1))
#Output - 2621680032944


So as soon as the value in s1 changes, s2 stops referring to s1.

This is why you are required to pass the variables into a table. But by using the ord() function, this function returns the Unicode of a character. This can be used to remove a character from a string. The code is as followed.

Limitations and Caveats

  • Python string comparison operators can only be used to compare objects of a similar type.
  • A best practice is to use == when working with immutables
Do let me know your thought in the comment section below :)