In this article we will discuss different ways to compare strings in python like, using == operator (with or without ignoring case) or
using is operator or using regex.

Python provides various operators to compare strings i.e. +, !=, <, >, <=, >=. When used for comparison these operators return Boolean True or False value. Let’s use these operators to compare strings.

Compare strings to check if they are equal using == operator using Python

Suppose we have two strings i.e.

Now let’s use == operator to match the contents of both the strings i.e.

Output:

As contents of both the strings were exactly same, so == operator returned True. But this is a case sensitive match. Now let’s see how to compare strings in case insensitive manner,

Compare strings by ignoring case using Python

Suppose we have two strings i.e.

As both the strings has similar characters but in different case. So to match these strings by ignoring case we need to convert both strings to lower case and then match using operator == i.e.

Output:

It matched the strings in case in sensitive manner.

Check if strings are not equal using != operator using Python

To confirm if the contents of two strings are not same we can use != operator too. Let’s see an example,

Suppose we have two strings i.e.

Now let’s check if both strings contains different text i.e.

Output:

As contents of both the strings were different, so operator != returned True.

Check if one string is greater than or less than other string.

Operator <, > , <=, >= compares the strings in alphabetical order. For example

  • “abcd” is greater than “abcc”
  • “abc” is less than “Abc”
  • “abcdd” is greater than “abc”

Let’s see actual example,

Output:

Similarly we can use <= & >= operator to compare strings in lexical order.

Comparing strings : is vs == Operator

Sometimes is operator is also used to compare strings to check if they are equal or not. But it will not always work because there is a fundamental difference in functionality of is and == operator in python.

is operator

It’s used to checks for the equality of two objects i.e. if two given variables points to same object or not. Let’s understand by examples,
Suppose we have two string objects i.e.

Now both the variables firstStr & secStr points to same object. We can confirm by print their object ID’s i.e.

Output:

Now we will compare these two string variable using is operator, then it will check if both the variables points to same object internally i.e.

Output:

As both variables were referring to same object, so is operator returned True.

Now let’s change the second variable secStr i.e.

Now print the contents and object ID of both the objects i.e.

Output:

Contents of both the object is same but both are referring to different object. Now let’s compare these using is operator i.e.

Output:

Although, both the strings had same contents but they were referring to different objects internally, so is operator returned False.

Compare contents using == operator

So, to compare and check if the contents of a two strings are equal, we should use == operator i.e. for above string objects == operator will return True i.e.

Output:

Compare strings using Regex in Python

Python provides a module re for regular expression matching operations. We can use that to compare strings using regex patterns.

Suppose we have few IPs like,

and we want to check if all these IP matches the sub-net mask of “192.122.78.XXX” using regex module of python.

For that first of all import the re module i.e.

Now let’s create a regex pattern to match the IP strings with subnet mask i.e.

It returns a Pattern object. We can use the Pattern.fullmatch(string[, pos[, endpos]]) to check if whole string matches this regular expression i.e.

Output:

It shows which IP strings completely matches our regex pattern.

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.