DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Updated on • Originally published at flexiple.com

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.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Content

Python String Comparison

Python string comparison methods are used to compare strings. However, Python also comes with a few handy inbuilt operators to facilitate this. However before we dive into the methods, we have an important concept that needs to be addressed.

Data in your program are represented as objects, and object has these 3 properties. Identity (Id) - Identity contains the address of the memory in which the data is stored. Next, Type is the data type of the object and Value is the content that the object stores.

Python saves memory by re-using object IDs containing the same value; eg: here. This also makes python string comparison faster and easier. Also, please be vary these terms as each operator uses a property to compare objects., this makes python string comparison much faster and easier.

String Comparison Operators

Out of the different methods that can be used to compare strings in python, I've explained two of the most commonly used methods below.

Note: All these comparison methods return a boolean true or false.

"==" and "!="

The == and != are commonly used relation operations for python string comparison. These operators compare Unicode values of all the elements in the string and return either a boolean true or false.

Unicode values are python's way of storing string. Each element of a string is given a Unicode, this helps keep elements uniform irrespective of the language the programmer uses. You can read more about this here.

So these relation operators compare strings based on their Unicode values.

Using "=="

The "==" is a python string comparison method that checks if both the values of the operands are equal. This operator is the most commonly used method to check equality in python.

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
Enter fullscreen mode Exit fullscreen mode

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 based on you compiler.

Using "!="

The != is another python string comparison operator that checks if the values of the operands are not equal. It performs the opposite of == operator. The code snippet below is the implementation of the same.

s1 = 'flexiple!'

s2 = 'flexiple!'

s3 = 'flexiple'

print(s1!=s2)
#Output = False
print(s2!=s3)
#Output = True
Enter fullscreen mode Exit fullscreen mode

"is" and "not is"

The is and not is operators are quite similar to == and != respectively. However, unlike the relational operators, is and is not compares to the Identity (id) of the objects and returns true 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 allocates changes giving it a new identity.

This is something you need to keep in mind while using the is and is not operator for comparison.

Using "is":

s1 = 'flexiple!'

s2 = 'flexiple!'

s3 = 'flexiple'

print(s1 is s2)
#output = True
print(s2 is s3)
#output = False
Enter fullscreen mode Exit fullscreen mode

Using "is not":

s1 = 'flexiple!'

s2 = 'flexiple!'

s3 = 'flexiple'

print(s1 is not s2)
#output = False
print(s2 is not s3)
#output = True
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

When the value of s1 changes, s2 immediately stops referring to s1 and is given a new identity.

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

Top comments (0)