Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 18, 2021 11:19 am GMT

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 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, I have added an example below, 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 = 2621679855024s2 = 'flexiple!'print(id(s2))#Output = 2621679855024s3 = 'flexiple'print(id(31))#Output = 140735453670112print(s1==s2)#output = Trueprint(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 = Falseprint(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 = Trueprint(s2 is s3)#output = False

Using "is not":

s1 = 'flexiple!'s2 = 'flexiple!'s3 = 'flexiple'print(s1 is not s2)#output = Falseprint(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 = 2621679855024s2 = 'flexiple!'print(id(s2))#Output = 2621679855024#Now let us update s1s1 = 'flexi'print(id(s1))#Output - 2621680032944

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

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

Original Link: https://dev.to/hrishikesh1990/various-methods-for-python-string-comparison-41fj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To