Show
In this article we will discuss different ways to compare strings in python like, using == operator (with or without ignoring case) or 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 PythonSuppose we have two strings i.e. 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 PythonSuppose we have two strings i.e. It matched the strings in case in sensitive manner. Check if strings are not equal using != operator using PythonTo 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. 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
Let’s see actual example, Similarly we can use <= & >= operator to compare strings in lexical order. Comparing strings : is vs == OperatorSometimes 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 operatorIt’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. firstStr = "sample" secStr = "sample"Now both the variables firstStr & secStr points to same object. We can confirm by print their object ID’s i.e. if firstStr is secStr: print('Both the objects are equal i.e. points to same object') print("Object ID of First object :" , id(firstStr)) print("Object ID of Second object :", id(secStr)) Output:Both the objects are equal i.e. points to same object Object ID of First object : 53526272 Object ID of Second object : 53526272 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.if firstStr is secStr: print('Both the objects are equal') Output:Both the objects are equal. As both variables were referring to same object, so is operator returned True. Now let’s change the second variable secStr i.e. Contents of both the object is same but both are referring to different object. Now let’s compare these using is operator i.e. if firstStr is secStr: print('Both the objects are equal i.e. points to same object') else: print('Both the objects are different') Output:Both the objects are different Although, both the strings had same contents but they were referring to different objects internally, so is operator returned False. Compare contents using == operatorSo, 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. Compare strings using Regex in PythonPython 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. It shows which IP strings completely matches our regex pattern. Complete example is as follows, Output: Compare to check if two strings are equal Both Strings are same Compare two strings by ignoring case Both Strings are same Compare to check if two strings are not equal using != operator Both Strings are not equal Check if one string is greater or less than other string "abcd" is greater than "abcc" "Abc" is less than "abc" "abcdd" is greater than "abc" Comparing strings : is vs == operator Both the objects are equal i.e. points to same object Object ID of First object : 50904832 Object ID of Second object : 50904832 firstStr: sample : Object ID : 50904832 secStr: sample : Object ID : 10743104 Both the objects are different Contents of both Strings are same Comparing strings Using regex Contents of string 192.122.78.11 matched the pattern Contents of string 192.122.78.305 matched the pattern Contents of string 192.122.77.111 do not matched the pattern |