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 o…


This content originally appeared on DEV Community and was authored by hrishikesh1990

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 = 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":

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.

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


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-05-18T11:19:09+00:00) Various methods for Python String Comparison. Retrieved from https://www.scien.cx/2021/05/18/various-methods-for-python-string-comparison/

MLA
" » Various methods for Python String Comparison." hrishikesh1990 | Sciencx - Tuesday May 18, 2021, https://www.scien.cx/2021/05/18/various-methods-for-python-string-comparison/
HARVARD
hrishikesh1990 | Sciencx Tuesday May 18, 2021 » Various methods for Python String Comparison., viewed ,<https://www.scien.cx/2021/05/18/various-methods-for-python-string-comparison/>
VANCOUVER
hrishikesh1990 | Sciencx - » Various methods for Python String Comparison. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/18/various-methods-for-python-string-comparison/
CHICAGO
" » Various methods for Python String Comparison." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/05/18/various-methods-for-python-string-comparison/
IEEE
" » Various methods for Python String Comparison." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/05/18/various-methods-for-python-string-comparison/. [Accessed: ]
rf:citation
» Various methods for Python String Comparison | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/05/18/various-methods-for-python-string-comparison/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.