Hit enter after type your search item

How to compare strings in Python?

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

One way is using the comparison operators == and != (equal to and not equal to). A few other methods are also explained in the later part of this tutorial.

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 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:

See online demo and code


comparision ==

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

An example with user input

Give a try to this example by copy/paste 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:

See online demo and code


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 next screen, otherwise, login failure message is displayed.

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:

See online demo and code


compare strings not equal

What if characters are same but 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 sort function and then compare both strings.

For example, consider this string:

Str1 = “Hello and Welcome”

Str1 = “Welcome and Hello”

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

See online demo and code


compare strings sorted

You may learn about the sorted method here.

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 same for both strings, however, object ID is different.

The following example gives a better idea:

See online demo and code


compare strings is operator

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

This div height required for enabling the sticky sidebar