Various methods for Python String ComparisonIn 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
Python String ComparisonPython 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 OperatorsOut 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.
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.
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.
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.
"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 not":
table - The values could either be a dictionary or a mapping table that describes what values should be replaced.
Now let's look at how the Identity (id) changes when the value is changing.
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