How to compare two strings in Python?

Strings are sequence of characters in python, enclosed within the single or double quote. For eg. "Python" is a string. There are different ways to compare strings. Some of them are as follows.

Comparing strings to check if they refer to the same object using is and is not operator

The is and is not operator compares those strings to check if they refer to the same object and return a boolean value. The below code illustrates the use of is and is not operator to compare the strings.

str_1 = "Python"
str_2 = str_1
print("Id of str_1", id(str_1))
print("Id of str_2", id(str_2))
print("Str_1 is str_2 :", str_1 is str_2)
str_3 = "python"
print("Id of str_3", id(str_3))
print("str_1 is not str_3 :", str_1 is not str_3)

Output

Id of str_1 2083511823536
Id of str_2 2083511823536
Str_1 is str_2 : True
Id of str_3 2083511888304
str_1 is not str_3 : True

Comparing strings using a relational operator

The relational operator returns a boolean value by comparing the ASCII values of the characters in two strings. Consider a string string_1 = "Python" and string_2 = "python". The operations string_1 > string_2 returns False, as the ASCII value of uppercase 'P' is 80 and lowercase 'p' is 112.

When two strings are compared using a relational operator the ordering of the characters of strings is taken into consideration.

The following example illustrates the use of the relational operator to compare two strings.

string_1 = "Python"
string_2 = "python"
print("string_1 : ", string_1)
print("string_2 : ", string_2)
print("string_1 > string_2 : ", string_1 > string_2 )
print("string_1 == string_2 : ", string_1 == string_2 )
print("string_1 < string_2 : ", string_1 < string_2 )

The above code gives output as

string_1 :  Python
string_2 :  python
string_1 > string_2 :  False 
string_1 == string_2 :  False
string_1 < string_2 :  True 

Comparing strings to check if they are equal and are in the same order using for loop

The below code checks if two strings are equal, and are in the same order. There are two cases.

Case 1 : If the length of the strings is not equal, if block is executed and output is printed as string_1 not equal to string_2.

Case 2 : If the length of the strings is equal, a for loop iterates over the string_1 and string_2. If the character at any index is different, the code returns the output as string_1 not equal to string_2, and the break statement breaks the loop.

If the characters at every index are the same then the code returns the output as string_1 equal to string_2.

string_1 = "Python"
string_2 = "Python"
print("string_1 : ", string_1)
print("string_2 : ", string_2)
print("Length of string_1 : ", len(string_1))
print("Length of string_2 : ", len(string_2))
if len(string_1) != len(string_2):
    print("string_1 not equal to string_2")
else:
    flag = 0
    for i in range(len(string_1)):
        if string_1[i]!= string_2[i]:
               flag = 1
               print("string_1 not equal to string_2")
               break
    if flag == 0:
        print("string_1 is equal to string_2")

Output

string_1 :  Python
string_2 :  Python
Length of string_1 :  6
Length of string_2 :  6
string_1 is equal to string_2

Comparing strings to check if they have the same characters

The set() function can convert the string to set data type and removes the duplicate elements. The == operator compares two sets to check if both strings contain the same characters.

Consider two strings, string_1 = "python" and string_2 = "nohtyp". The set() function converts string_1 and string_2 to set data-type and the returns the result as set_1 : {'h', 'o', 'n', 'y', 'p', 't'} and set_2 : {'n', 'o', 'h', 'y', 'p', 't'}. The == operator compares set_1 and set_2. If the both sets are equal if block is executed and output is printed.

string_1 = "python"
string_2 = "nohtyp"
print("string_1 : ", string_1)
print("string_2 : ", string_2)
set_1 = set(string_1)
set_2 = set(string_2)
print("set_1 : ", set_1 )
print("set_2 : ", set_2)
if set_1 == set_2:
    print("strings have same characters")
else:
    print("strings doesn't have same characters")

Output

string_1 :  python
string_2 :  nohtyp
set_1 :  {'h', 'o', 'n', 'y', 'p', 't'}
set_2 :  {'n', 'o', 'h', 'y', 'p', 't'}
strings have same characters

Comparing strings to check if they have the same characters with equal frequencies

The sorted() function sorts the characters of the strings and returns an iterable. The iterable can be compared with the == operator to check if both strings have the same characters with equal frequencies.

Consider two strings string_1 = "Chercher" and string_2 =" cherCher". The sorted() function sorts the string_1 and string_2 according to unicode values and returns the output as list_1 : ['C', 'c', 'e', 'e', 'h', 'h', 'r', 'r'] and list_2 : ['C', 'c', 'e', 'e', 'h', 'h', 'r', 'r'].

The == operator compares both the iterable to check if strings contain characters with equal frequencies.

string_1 = "Chercher"
string_2 = "cherCher"
print("string_1 : ", string_1)
print("string_2 : ", string_2)
list_1 = list(sorted(string_1))
list_2 = list(sorted(string_2))
print("list_1 : ", list_1 )
print("list_2 : ", list_2)
if list_1 == list_2:
    print("strings have characters with equal frequencies")
else:
    print("strings doesn't have characters with equal frequencies")

Output

string_1 :  Chercher
string_2 :  cherCher
list_1 :  ['C', 'c', 'e', 'e', 'h', 'h', 'r', 'r']
list_2 :  ['C', 'c', 'e', 'e', 'h', 'h', 'r', 'r']
strings have characters with equal frequencies
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions