Skip to content
Related Articles

Related Articles

String Comparison in Python
  • Difficulty Level : Basic
  • Last Updated : 25 Oct, 2020

Let us see how to compare Strings in Python. 

Method 1: Using Relational Operators

The relational operators compare the Unicode values of the characters of the strings from the zeroth index till the end of the string. It then returns a boolean value according to the operator used.


“Geek” == “Geek” will return True as the Unicode of all the characters are equal

In case of “Geek” and “geek” as the unicode of G is \u0047 and of g is \u0067
“Geek” < “geek” will return True and
“Geek” > “geek” will return False


print("Geek" == "Geek")
print("Geek" < "geek")
print("Geek" > "geek")
print("Geek" != "Geek")



Method 2: Using is and is not

The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not. The same is the case for != and is not.

Let us understand this with an example:


str1 = "Geek"
str2 = "Geek"
str3 = str1
print("ID of str1 =", hex(id(str1)))
print("ID of str2 =", hex(id(str2)))
print("ID of str3 =", hex(id(str3)))
print(str1 is str1)
print(str1 is str2)
print(str1 is str3)
str1 += "s"
str4 = "Geeks"
print("\nID of changed str1 =", hex(id(str1)))
print("ID of str4 =", hex(id(str4)))
print(str1 is str4)


ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0

The object ID of the strings may vary on different machines. The object IDs of str1, str2 and str3 were the same therefore they the result is True in all the cases. After the object id of str1 is changed, the result of str1 and str2 will be false. Even after creating str4 with the same contents as in the new str1, the answer will be false as their object IDs are different.

Vice-versa will happen with is not.

Method 3: Creating a user-defined function.

By using relational operators we can only compare the strings by their unicodes. In order to compare two strings according to some other parameters, we can make user-defined functions.

In the following code, our user-defined function will compare the strings based upon the number of digits.


# function to compare string
# based on the number of digits
def compare_strings(str1, str2):
    count1 = 0
    count2 = 0
    for i in range(len(str1)):
        if str1[i] >= "0" and str1[i] <= "9":
            count1 += 1
    for i in range(len(str2)):
        if str2[i] >= "0" and str2[i] <= "9":
            count2 += 1
    return count1 == count2
print(compare_strings("123", "12345"))
print(compare_strings("12345", "geeks"))
print(compare_strings("12geeks", "geeks12"))



 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :