String Equals Check in Python

In Python, a data type named “string” is used to store the sequence of characters inside the single or double quotations. Python’s built-in functions and operators can be utilized to perform a number of operations on strings. For instance, to check whether the given string values are equal or not, different methods are used in python, such as the “is” operator, “ __eq__()” Function, etc.

This post will provide an in-depth guide on how to check or compare whether the strings are equal in Python or not using the below methods:

Method 1: Using Python ‘is’ Operator

The Python “is” operator is used in the below code to check the string equality:

Code:

first_string = "Linux"
second_string = "Python"
third_string = "Linux"

if(first_string is second_string):
print("First_string is equal to Second_string")

elif(first_string is third_string):
print("First_string is equal to Third_string")

elif(first_string is third_string):
print("First_string is equal to Third_string")

else:
print("Second_string is equal to Third_string")
  • Multiple strings are initialized in the variables named “first_string”, “second_string” and “third_string‘.
  • The “if-elif-else” statements are used along with “is operator” to check whether the given string values are equal or not.

Output:

The first string is equal to the third string.

Method 2: Using Python ‘==’ Operator

The below code uses the Python “==” operator to perform the string comparison:

Code:

first_string = "Linux"
second_string = "Linux"
third_string = "Ubuntu"

print('First String is Equal to Second String: ', first_string == second_string)
print('\nFirst String is Equal to Third String: ',first_string == third_string)
print('\nSecond String is Equal to Third String: ',second_string == third_string)
  • The strings named “first_string”, “second_string” and “third_string‘ are initialized.
  • The “==” operator is used to check whether the initialized strings are equal.

Output:

Method 3: Using Python ‘!=’ Operator

The “!=” operator is utilized in the below code to check if the provided strings are equal or not:

Code:

string1 = "Python"
string2 = "Python"

if(string1 != string2):
print("string1 is not equal to string2")

else:
print("string1 is equal to string2")
  • Firstly, we initialized the string.
  • The “!=” operator is utilized to verify whether the input strings are equal/similar or not.
  • The “!=” operator returns “True” when the given strings are not equal.

Output:

Method 4: Using Python “__eq__()” Function

The Python “__eq__()” function is used in the below code to compare the strings:

Code:

string_value1 = "Linux"
string_value2 = "Python"

if(string_value1.__eq__(string_value2)):
print("string_value1 is equal to string_value2")

else:
print("string_value1 is not equal to string_value2")

The “if statement” is used along with the “__eq__()” function to check whether the given strings are equal to each other or not.

Output:

How to Perform Case-Sensitive Strings Comparison in Python?

The “casefold()” function is used along with the Python “__eq__()” function to check or compare the given string’s value case-insensitively. Here is an example code:

Code:

string_value1 = "Linux"
string_value2 = "LINUX"

if((string_value1.casefold()).__eq__(string_value2.casefold())):
print("string_value1 is equal to string_value2")

else:
print("string_value1 is not equal to string_value2")
  • The case sensitive strings “Linux” and “LINUX” are initialized.
  • The “casefold()” function is used with the given string values to convert all the characters into lowercase strings.
  • The lowercase string values are checked using the “__eq__()” function.

Output: 

Conclusion

To check or compare the strings’ equality, the “is” operator, “==” operator, “‘!=” operator, and “ __eq__()” function is used in Python. Python is case-sensitive, so the “casefold()” function can compare the initialized strings case-insensitively. This guide presented an in-depth guide on how to check string equality in Python using numerous examples.