Python Program to check two strings are equal or not

In this tutorial, we are going to learn writing a python program to check if two given strings given input by the user are the same or not.

When working with text in Python, a common task is checking if two strings are the same. This means seeing if the characters in one string are exactly like the characters in another string, in the same order. This can be useful in many situations, like when making sure passwords match or confirming entries in a form are correct. Python provides straightforward methods to perform this check, each with its own use depending on what you need for your program. Let’s explore different ways to determine if two strings are equal.

Problem Statement

For any two input strings, we have to check if strings are same or not.

For example:

Case1: If the user has given the input string ‘Quescol’ as the first string and ‘Quescol’ as the second string,

The output should be ‘Both strings are same’.

Case2: If the user input the string ‘Quescol’ as the first string and ‘quescol’ as the second string,

The output should be ‘Both strings are not same’.

Case3: If the user input the string ‘Quescol’ as the first string and ‘website’ as the second string,

The output should be ‘Both strings are not same’.

Logic to check if two strings are the same

  • Firstly, we need to take two strings as input from the user using the input() function.
  • Then, compare the strings using the comparison operator. If it returns true then the two strings are the same.

Algorithm to check if two strings are the same(case sensitivity)

Step1:  Start

Step2:   Take two strings as input from the user.

Step3: if string_1 == string_2:

                 print strings are equal

            else:

                print strings are equal

Step4: Stop

Python Program to check Strings are the same

Program 1: Using Equality(==) operator

The simplest way to compare two strings is by using the equality operator (==). This operator checks if the contents of both strings are identical and returns True if they are, or False otherwise.

#taking two strings as the input from the user
str1 = input('Enter first string: ')
str2 = input('Enter second string: ')
#comparing two strings
if str1== str2:
    		print("The strings are the same")
else:
    print("The strings are not same") 

Output

Enter first string: python
Enter second string: python
The strings are the same

Explanation

In this example, the first string is taken as ‘Quescol’ and the second string is ‘quescol’, according to case sensitivity, the capital letter and small letter alphabet are considered as different, so according to the output generated ‘The strings are not same’ is correct.

Program 2: Using the is Keyword

While the is keyword is generally used to determine if two variables point to the same object (identity comparison), it’s not typically used for content equality but can be demonstrated for educational purposes.

#taking two strings as the input from the user
str1 = input('Enter first string: ')
str2 = input('Enter second string: ')

print(str1 is str2)   

Output

Enter first string: Python
Enter second string: python
False

Program 3: Case-Insensitive Comparison

Sometimes, you might want to check if two strings are equal regardless of their case (uppercase or lowercase). You can do this by converting both strings to the same case before comparing.

#taking two strings as the input from the user
str1 = input('Enter first string: ')
str2 = input('Enter second string: ')
print(str1.lower() == str2.lower())  

Output

Enter first string: Python
Enter second string: python
True

Program 4: Using str.casefold() for Aggressive Lowercasing

casefold() is similar to lower(), but it is more aggressive and designed for cases where you want to make the program more robust to handle different languages and case conventions.

#taking two strings as the input from the user
str1 = input('Enter first string: ')
str2 = input('Enter second string: ')
print(str1.casefold() == str2.casefold()) 

Output

Enter first string: Python
Enter second string: python
True

Conclusion

Comparing strings to check if they are equal is a basic operation in Python but very important in many applications. Whether you’re verifying user input, comparing text data, or ensuring conditions are met before proceeding with a task, knowing how to effectively compare strings is essential. We’ve seen several methods to accomplish this, from using simple comparison operators to employing functions that handle cases and spaces. By understanding these techniques, you can ensure your Python programs can accurately compare strings and react accordingly based on whether they match. This knowledge is invaluable for anyone starting with Python or looking to refine their programming skills in handling text data.