In this article, we will be having a look at the different ways to perform a string equals check in Python.
String comparison is basically the comparison of two strings i.e. it the procedure through which we check for the equality of strings by comparing the strings character-by-character.
Technique 1: Python ‘==’ operator to check the equality of two strings
Python Comparison operators can be used to compare two strings and check for their equality in a case-sensitive manner
i.e. uppercase letters and lowercase letters would be treated differently.
Python '==' operator
compares the string in a character-by-character manner and returns True if the two strings are equal, otherwise, it returns False.
Syntax:
string1 == string2
Example:
str1 = "Python"
str2 = "Python"
str3 = "Java"
print(str1 == str2)
print(str1 == str3)
Output:
True
False
Technique 2: Python ‘!=’ operator for String comparison
Python ‘!=’ operator can also be used to perform a string equals check in python.
The '!=' operator
compares two strings and returns True if the strings are unequal, otherwise, it returns False.
Syntax:
string1 != string2
Example:
str1 = "Python"
str2 = "Python"
str3 = "Java"
if(str1 != str3):
print("str1 is not equal to str3")
if(str1 != str2):
print("str1 is not equal to str2")
else:
print("str1 is equal to str2")
Output:
str1 is not equal to str3
str1 is equal to str2
Technique 3: Python ‘is’ operator to perform string equals check in python
Python “is” operator can be used to efficiently check for the equality of two string objects. The is operator
returns True if the two variables point to the same data object, else, it returns False.
Syntax:
variable1 is variable2
Example:
str1 = "Python"
str2 = "Python"
str3 = "Java"
if(str1 is str3):
print("str1 is equal to str3")
else:
print("str1 is not equal to str3")
if(str1 is str2):
print("str1 is equal to str2")
else:
print("str1 is not equal to str2")
Output:
str1 is not equal to str3
str1 is equal to str2
Technique 4: The __eq__() function to perform string equals check in python
Python in-built __eq__() method can be used to compare two string objects. The __eq__()
method basically compares two objects and returns True if found equal, otherwise, it returns False.
Syntax:
string1.__eq__(string2)
Example:
str1 = "Python"
str2 = "Python"
str3 = "Java"
if(str1.__eq__(str3)):
print("str1 is equal to str3")
else:
print("str1 is not equal to str3")
if(str1.__eq__(str2)):
print("str1 is equal to str2")
else:
print("str1 is not equal to str2")
Output:
str1 is not equal to str3
str1 is equal to str2
String equals check in Python : caseless comparison
str1 = "Python"
str2 = "PYTHON"
if(str1.__eq__(str2)):
print("str1 is equal to str2")
else:
print("str1 is not equal to str2")
Output:
str1 is not equal to str2
As seen in the above example, the result turns out to be FALSE, because the comparison is Case-sensitive
.
In order to have a caseless string comparison, i.e. in a case-insensitive
manner, then we can use Python string.casefold() function to serve the purpose.
The string.casefold()
method converts the string to lowercase instantly.
In the scenario of string comparison, we can pass both of the input strings to the casefold() function. Thus, both the string would be converted to lowercase and thus, we can have a caseless comparison.
Syntax:
string.casefold()
Example 2:
str1 = "Python"
str2 = "PYTHON"
str3 = "PYthoN"
if((str1.casefold()).__eq__(str2.casefold())):
print("str1 is equal to str2")
else:
print("str1 is not equal to str2")
if((str1.casefold()) == (str3.casefold())):
print("str1 is equal to str3")
else:
print("str1 is not equal to str3")
Output:
str1 is equal to str2
str1 is equal to str3
Conclusion
Thus, in this article, we have understood ways techniques of case and caseless String comparison in Python.
References
- Python String comparison – JournalDev
- Python is operator – StackOverFlow
- Python Strings equal – JournalDev