How to Check if Two Strings are equal in Python

Here we will develop a program how to check if two strings are equal in python both condition case-sensitive and case-insensitive. In computers, case-sensitivity defines whether uppercase and lowercase letters are treated as distinct (case-sensitive) or equivalent (case-insensitive).

How to Check if Two Strings are Same in Python

This python program using the if-else statement and equality operator (==) to check if two strings are equal or not. The == operator compares the value or equality of two objects. This python program only works for case-sensitive strings. Case-sensitive means text or typed input that is sensitive to the capitalization of letters. For example, “Computer” and “computer” are two different words because the “C” is uppercase in the first example and lowercase in the second example.

# Python program to check if two strings are equal

# first string
string1 = input('Enter first string: ')

# second string
string2 = input('Enter second string: ')

# check strings is equal or not
if(string1 == string2):
    print('The strings are the same.')
else:
    print('The strings are not the same.')

Output for the different input values:-

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

Enter first string: know program
Enter second string: know program
The strings are the same.

Enter first string: KNOW PROGRAM
Enter second string: know program
The strings are not the same.

Python String equals ignore-case or case-insensitive

The case-insensitive means the string which you are comparing should exactly be the same as a string that is to be compared but both strings can be either in upper case or lower case. (ie., different cases)

This python program using the built-in function to check string is equal or not. The lower() function converts all uppercase characters in a string into lowercase characters and the if-else statement check string are equal or not using equality operator (==).

# Python program to compare strings ignore case

# first string
string1 = input('Enter first string: ')

# second string
string2 = input('Enter second string: ')
  
# check strings are same using lower() function
if(string1.lower() == string2.lower()):
    print('The strings are the same.')
else:
    print('The strings are not the same.')

Output for the different input values:-

Enter first string: KNOW PROGRAM
Enter second string: know program
The strings are the same.

Enter first string: know program
Enter second string: know program
The strings are the same.

The lower() function is not work in every condition. For example, the German lowercase letter ‘ß’ is equivalent to ‘ss’. But every user might not know German, However, since ‘ß’ is already lowercase, the lower() method does nothing to it. But, casefold() converts it to ‘ss’.

The casefold() method works similar to lower() and upper() method. But compared to the lower() and the upper() method it performs a strict string comparison by removing all case distinctions present in a string. It is used for caseless matching, i.e. ignores cases when comparing.

Also See:- Python Compare Strings ignore-case
Also See:- Python program to convert lowercase to uppercase

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *