Python String Comparison

In this tutorial we are going to see different methods by which we can compare strings in Python. We will also see some tricky cases when the python string comparison can fail and golden rules to get string comparison always right.

Python strings are Immutable. 

This means that once you have created a string then it cannot be modified, if you do modify it then it will create a new python string. Example below will explain the fact.

Here when we make change in str1 then the id of the string changes that confirms that a new string object is created. Now one more question remains i.e. why str1 and str2 have the same id ? 

That is because python do memory optimizations and similar string object are stored as only one object in memory. This is also the case with small python integers. 

Now getting to string comparison there are two different methods by which we can compare strings as below.

Python String Comparison

Method 1: Comparing Using is Operator

is and is not operator is used to compare two strings as below:

The two strings to be compared are written on either side of the is operator and the comparison is made. is operator compares string based on the memory location of the string and not based on the value stored in the string. 

Similarly to check if the two values are not equal the is not operator is used. 

Method 2: Comparing Using == Operator

The == operator is used to compare two strings based on the value stored in the strings. It’s use is similar to is operator.

Similarly to check if the two strings are not equal the != is used. 

Why the python string being immutable is important?

Even the python strings weren’t immutable then a comparison based on the memory location would not be possible and therefore is and is not operator cannot be used. 

Both of the comparison methods above are case sensitive i.e. ‘Cat’ and ‘cat’ are treated differently. Function below can be used to first convert the string in some particular case and then use them.

  • .lower() : makes the string lowercase 
  • .upper() : makes the string uppercase

So if both strings are first converted into a similar case and then checked then it would make the comparison case insensitive indirectly. Example below will make things more clear. 

The golden line to remember whenever using the == and is operator is 

== used to compare values and is used to compare identities.

One more thing to remember here is:

if x is y is then x == y is true

It is easily understandable as x and y points to the same memory locations then they must have the same value at that memory location. But the converse is not true. Here is an example to support the same:

In this example c is at a new memory location and that’s why a is c prints false.

Leave a Reply

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