String Equals Check in Python: Using 4 Different Methods

String Equals Check In Python

In programming, string comparison is a fundamental operation used to determine whether two strings are equal.

Suppose you are building an application where you want to find a user using his/her username, the username stored in the database is a string and the keyword to be searched is also a string. So you would have to implement some functionality to detect that both strings are the same. Here comes the string comparison.

In this tutorial, we will learn how to compare strings using four different methods so you can choose any one as per your requirement.

Python String Comparison

In Python, string comparison is basically a process through which we compare the strings character-by-character to check for equality.

We can compare strings using several ways like using ‘==’ operator, ‘!=’ operator, ‘is’ operator and __eq__() function. Let’s look at them one by one.

Python ‘==’ Operator to Check the Equality of Two Strings

Python Comparison operators can be used to compare 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 two strings in a character-by-character manner and returns True if both strings are equal, otherwise, it returns False.

Syntax:

string1 == string2

Example:

s1 = "Python"
s2 = "Python"
s3 = "Java"

print(s1 == s2)
print(s1 == s3)

Output:

True
False

Here we can see that s1 is equal to another string s2 so True is printed to the console, whereas when we compared s1 with s3 we got False.

Python ‘!=’ Operator for String Comparison

Python ‘!=’ operator can also be used to perform a string equals check. Its return value is the opposite of the return value obtained using the ‘=’ operator.

The ‘!=’ operator compares two strings and returns True if the strings are unequal, otherwise, it returns False.

Syntax:

string1 != string2

Example:

s1 = "Python"
s2 = "Python"
s3 = "Java"

if(s1 != s3):
 print("s1 is not equal to s3")

if(s1 != s2):
 print("s1 is not equal to s2")
else:
 print("s1 is equal to s2")

Output:

s1 is not equal to s3
s1 is equal to s2

Python ‘is’ Operator for String Comparison

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:

string1 is string2

Example:

s1 = "Python"
s2 = "Python"
s3 = "Java"

if(s1 is s3):
 print("s1 is equal to s3")
else:
 print("s1 is not equal to s3")
 
if(s1 is s2):
 print("s1 is equal to s2")
else:
 print("s1 is not equal to s2")

Output:

s1 is not equal to s3
s1 is equal to s2

Also Read: Difference between “is” and “==” in Python

Python __eq__() Function to perform String Equals Check

Python in-built __eq__() method can be used to compare two string objects. The __eq__() method basically compares two objects and returns a boolean True or False. It returns True if strings are equivalent, 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 Matching

If we use any of the above methods, they check the equality of strings using case-sensitive comparison. Let’s see with the help of an example.

Example:

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.

So how can we check that strings are equal in a case-insensitive comparison? 

In order to have a caseless string comparison, i.e. in a case-insensitive manner, we can use the Python string.casefold() function.

The string.casefold() function converts the string to lowercase instantly. It returns a casefolded copy of the string which can be used for caseless matching.

Syntax:

string.casefold()

In this scenario of string comparison, we can pass both the input strings to the casefold() function. After which both the string will be converted to lowercase and thus, we can have a caseless comparison.

Example:

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

In this tutorial, we learned how to compare one string with another string in Python using four methods, and we have also implemented a case-insensitive approach to check string equality. The method mostly used by developers is the ‘==’ operator as it is very simple to implement and does the required job well. Hope you enjoyed reading our content.

References