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. Show
Table of Contents
Python String ComparisonPython 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: . 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 OperatorsOut 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. Python strings comparison using "==" 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.
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.
Python strings comparison using "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. When crafting the logic in your code, you may want to execute different commands depending on the similarities or differences between two or more strings. In this article, we'll see various operators that can help us check if strings are equal or not. If two strings are equal, the value returned would be How to Check for String Equality in PythonIn this section, we'll see examples of how we can compare strings using a few operators. But before that, you need to have the following in mind:
How to Compare Strings Using the == OperatorThe
We got a value of Let's make it look a bit more fancy using some conditional logic:
In the code above, we created two strings and stored them in variables. We then compared their values. If these values are the same, we would get one message printed to the console and if they aren't the same, we would have a different message printed. Both strings in our case were equal, so we had "Both strings are equal" printed. If we changed the first string to "hello", then we would have a different message. Note that using 2 would make the interpreter assume you want to assign one value to another. So make sure you use == for comparison.How to Compare Strings Using the string1 = "Hello" string2 = "Hello" if string1 == string2: print("Both strings are equal") else: print("Both strings are not equal") # Both strings are equal4 OperatorThe 4 operator checks if two strings are not equal.
We're using the same example but with a different operator. The 4 is saying the strings are not equal which is False so a message is printed based on those conditions.I have commented the code to help you understand better. How to Compare Strings Using the string1 = "Hello" string2 = "Hello" if string1 == string2: print("Both strings are equal") else: print("Both strings are not equal") # Both strings are equal8 OperatorThe 8 operator checks if one string is smaller than the other.
This returns We can also use conditional statements here like we did in previous sections. How to Compare Strings Using the string1 = "Hello" string2 = "Hello" if string1 != string2: print("Both strings are not equal") # return if true else: print("Both strings are equal") # return if false # Both strings are equal1 OperatorThe 1 operator checks if one string is less than or equal to another string.
Recall that this operator checks for two things – if one string is less or if both strings are the same – and would return We got How to Compare Strings Using the string1 = "Hello" string2 = "Hello" if string1 != string2: print("Both strings are not equal") # return if true else: print("Both strings are equal") # return if false # Both strings are equal5 OperatorThe 5 operator checks if one string is greater than another string.
Since the string on the left isn't greater than the one on the right, we got How to Compare Strings Using the string1 = "Hello" string2 = "Hello" if string1 != string2: print("Both strings are not equal") # return if true else: print("Both strings are equal") # return if false # Both strings are equal8 OperatorThe 8 operator checks if one string is greater than or equal to another string.
Since one of both conditions of the operator is true (both strings are equal), we got a value of ConclusionIn this article, we learned about the various operators you can use when checking the equality of strings in Python with examples. We also saw how case sensitivity can alter the equality of strings. Happy coding! ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT This author's bio can be found in his articles! If you read this far, tweet to the author to show them you care. Tweet a thanks Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started How do you check if two strings are similar in Python?Python strings equality can be checked using == operator or __eq__() function. Python strings are case sensitive, so these equality check methods are also case sensitive.
Can I use == to compare two strings?You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
How do you compare 2 strings?We compare the strings by using the strcmp() function, i.e., strcmp(str1,str2). This function will compare both the strings str1 and str2. If the function returns 0 value means that both the strings are same, otherwise the strings are not equal.
|