How To Check If Two Strings Are Equal In Python

This article will teach you how to check if two Strings are equal in Python. I’ll go over four methods for determining whether two strings are similar or not.

Determining whether or not two strings are equal by comparing them character by character is known as a “string comparison.”

1. Using Python Equals Operator “==”

To determine if two strings are the same or not, Python provides case-sensitive comparison operators that take into account the case of each character.

When using Python’s “==” operator, strings are compared character by character and if all the characters in both the strings are the same it returns True or will simply return False.

Let us see the below code example to compare two strings using the “==” operator.

#Initializing Two String Variables
import string


string1 = "Test String"
string2 = "Test String"
string3 = "Hello"

#Checking if the above string variable is similar
if string1 == string2:
    print("Both '{}' and '{}' string are same.".format(string1, string2))
else:
    print("Both '{}' and '{}' string are different".format(string1, string2))

#Or you can just Print True false using "==" operator.
print(string1==string3)

Output:

Both 'Test String' and 'Test String' string are same.
False

As shown in the preceding code, you can use the “==” operator with an if statement and then print or do something based on the result, or you can use the “==” operator in a print statement to print True or False.

2. Using “is” Operator To Check If Two Strings Are Equal In Python

The “is” operator in Python is useful for quickly testing whether or not two string objects are equal. If the values of the two variables refer to the same data object, then the is operator will return True; otherwise, it will return False.

Let us see in the below code example how you can use the “is” operator to check two strings.

#Initializing Two String Variables
import string


string1 = "Learn Python"
string2 = "Learn Python"
string3 = "Learn Java"

#Checking if the above string variable is similar
if string1 is string2:
    print("Both '{}' and '{}' string are same.".format(string1, string2))
else:
    print("Both '{}' and '{}' string are different".format(string1, string2))

#Or you can just Print True false using "is" operator.
print(string1 is string3)

Output:

Both 'Learn Python' and 'Learn Python' string are same.
False

We were able to successfully compare two strings using the “is” operator in the preceding code example. It is possible to accomplish this using an if statement, and you can also use the print statement to print True or False.

3. Using __eq__() Function To Compare Strings In Python

The __eq__() method that is built into Python can be used to perform a comparison between two string objects.

In its simplest form, the __eq__() method checks to see if two objects are the same and, if they are, it returns True; otherwise, it returns False.

Let us see in the below code example the usage of the __eq__() method to compare strings in Python.

#Initializing Two String Variables
import string


string1 = "Cal State"
string2 = "Cal State"
string3 = "CMU"

#Checking if the above string variable is similar using __eq__()
if string1.__eq__(string2):
    print("Both '{}' and '{}' string are same.".format(string1, string2))
else:
    print("Both '{}' and '{}' string are different".format(string1, string2))

Output:

Both 'Cal State' and 'Cal State' string are same.
False

As you can see in the above code I was able to successfully print the comparison of two strings if they are equal or not using the __eq__() method.

But you need to note that all these methods discussed above are case-sensitive comparisons.

If you have two strings that are identical but have different case-sensitive characters, the results may differ if you ignore the case sensitivity.

In this case, you must employ a different method, which is discussed further below.

We can use the Python string.casefold() function to perform a case-insensitive comparison of strings, or a case-insensitive comparison of strings.

The string.casefold() method performs an immediate case-conversion to lowercase. The casefold() function can be used in a string comparison scenario by providing both strings as input.

This allows for a case-insensitive comparison because both strings have been converted to lowercase.

#Initializing Two String Variables
import string


string1 = "Case"
string2 = "CASE"
string3 = "No CASE"

#Checking if the above string variable is similar
if string1.casefold().__eq__(string2.casefold()):
    print("Both '{}' and '{}' string are same.".format(string1, string2))
else:
    print("Both '{}' and '{}' string are Different".format(string1, string2))

#Or you can just Print True false using "casefold()" function.
print(string1.casefold().__eq__(string3.casefold()))

Output:

Both 'Case' and 'CASE' string are same.
False

4. Using The “!=” Operator To Compare Strings

You can also do an equals check on strings in Python using the ‘!=’ operator. The ‘!=’ operator performs a string comparison and returns If the strings are different, it returns True, otherwise, it returns False.

Let us see in the below code example usage of the “!=” operator on string comparison.

#Initializing Two String Variables
import string


string1 = "Python Basics"
string2 = "Python Basics"
string3 = "Learn Java"

#Checking if the above string variable is similar
if string1 != string2:
    print("Both '{}' and '{}' string are different.".format(string1, string2))
else:
    print("Both '{}' and '{}' string are same".format(string1, string2))

#Or you can just Print True false using "!=" operator.
print(string1 != string3)

Output:

Both 'Python Basics' and 'Python Basics' string are same
True

As you can see, the “!=” operator allowed me to easily compare two strings.

How To Check If Two Strings Are Equal In Python

Wrap Up For Check Two Strings Equal In Python

I hope you understood the above code examples and the four different methods for determining whether two strings are equal in Python.

It is always recommended that you use the operator to compare the string rather than the function because it is faster unless you have a specific need that can only be met by using the function.

Further Read:

  1. How To Sort Array In Python
  2. 9 Beginner Tips for Learning Python Programming
  3. Convert DateTime To Unix Timestamp In Python
  4. How To Get Column Names In Pandas Dataframe

Leave a Comment