How to compare strings in Python?

A comprehensive infographic outlining various methods for comparing strings in Python. Explore equality, Inequality, sorted and ==, and is operator comparisons.

In this tutorial, I will show you different ways of comparing two strings in Python programs. These are:

    • Equality Comparison (==)
    • Inequality Comparison (!=)
    • Using the ‘is’ operator
    • sorted function with (==)

First way Using the == (equal to) operator for comparing two strings

If you simply require comparing the values of two variables then you may use the ‘==’ operator.

If strings are the same, it evaluates as True, otherwise False.

Have a look at the following example where two strings are compared in an if statement:

An example of Python compare strings with ==

Two string variables are created which is followed by using the if statement. In the if statement, both variables are compared by using equal to operator. Have a look at the code and output:

#String compare in Python example

str_x = 'Hello & Welcome'

str_y = 'Hello & Welcome'

#comparing by ==

if str_x == str_y:

    print ("Same Strings")

else:

    print ("Different Strings")

Output:

Same Strings

You see, as both strings are matched, it returned as True.

An example with user input

Give a try to this example by copying/pasting the code in your shell. As you run this program, it will ask to enter two strings. After that, both strings are compared in the if statement:

#String compare in Python with input


str_input1 = input("Enter First String? ")

str_input2 = input("Enter Second String? ")


#comparing by ==

if str_input1 == str_input2:

    print ("First and second strings are same!")

else:

    print ("You entered different strings!")

python compare strings

This may be useful in scenarios like authenticating the login screen.

Suppose, a user is asked to enter the “Secret Question” answer in the text field. The entered value is then matched to the value in the database.

If both are compared and evaluated as true, you may authenticate the user and proceed to the next screen, otherwise, a login failure message is displayed.

Second way – Using != (not equal to) operator to match strings

Similarly, you may use the != operator for string comparison. In this case, both strings are compared and it will return True if both are not equal. If strings are equal, it will return False. Have a look at the following example:

str_input1 = input("Enter First String? ")

str_input2 = input("Enter Second String? ")



#comparing by != (not equal to)

if str_input1 != str_input2:

    print ("Both Strings are different!")

else:

    print ("You entered same strings!")

compare strings not equal

Third way – Using sorted function

What if the characters are the same but the order is not?

If the scenario is to check two strings equality even if the order of words or characters is different then you may first use the sorted function and then compare both strings.

For example, consider this string:

Str1 = “Hello and Welcome”

Str1 = “Welcome and Hello”

Let us see how the sorted function can be used to compare these two strings:

#comparison by using sorted function

str_a = "Hello and welcome"

str_b = "welcome and Hello"



if sorted(str_a) == sorted(str_b):

    print ("Both are same!")

else:

    print ("Not Same")

Output:

Both are same!

You may learn about the sorted method here.

Fourth way – using the ‘is’ operator

If you are sure that objects are of the same type then you may use the ‘is’ operator for string comparison. Otherwise, do not use it, as it will return False even if the values are the same for both strings, however, object ID is different.

The following example gives a better idea:

#comparison by using is operator

str_a = "Python"

str_b = "Python"



str_c = ''.join(['P', 'y', 't', 'h', 'o','n'])


print(str_a is str_b)

print("str_c = ",str_c ,"! comparison result = ", str_a is str_c)

Output:

True
str_c = Python ! comparison result = False

Even the value of str_c = Python, but the ‘is’ operator is evaluated as False. The reason is that the str_c object was built differently. But, the result for str_a and str_b is True.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!