How to Compare Strings ? | Ignore case | regex | is vs == operator in Python ?
In this article we will discuss various ways to compare strings in python.
Python provides various operators for comparing strings i.e less than(<), greater than(>), less than or equal to(<=), greater than or equal to(>=), not equal(!=), etc and whenever they are used they return Boolean values i.e True or False.
Compare strings using == operator to check if they are equal using python :
Let’s see it with an example:
#program : firststr = 'First' secondstr = 'second' if firststr == secondstr: Â Â Â print('Strings are same') else: Â Â Â print('Strings are not same')
Output: Strings are not same
As the content of both the string are not same so it returned False.
#Program : firststr = 'done' secondstr = 'done' if firststr == secondstr: Â Â Â print('Strings are same') else: Â Â Â print('Strings are not same')
Output: Strings are same
Here, as the content of both the string are not same so it returned True.
Compare strings by ignoring case using python :
Let’s see it with an example:
#program : firststr = 'PROGRAM' secondstr = 'program' if firststr.lower() == secondstr.lower(): Â Â Â print('Strings are same') else: Â Â Â print('Strings are not same')
Output : Strings are same
As we can see that both the strings are same but are in different case. Now let’s try with another operator.
Check if string are not equal using != operator using python :
Let’s see it with an example:
#Program : firststr = 'python' secondstr = 'program' if firststr != secondstr:Â Â Â print('Strings are not same') else:Â Â Â print('Strings are same')
Output: Strings are not same
Here, the two strings are not same so it returned True.
Let’s try some other operators.
Check if one string is less than or greater than the other string :
Let’s see it with an example:
#Program : if 45 > 29: Â Â Â print('"45" is greater than "29"') if "abc" > "abb": Â Â Â print('"abc" is greater than "abb"') if "ABC" < "abc": Â Â Â print('"ABC" is less than "abc"') if 32 >= 32: Â Â Â print('Both are equal') if 62 <= 65: Â Â Â print('"62" is less than 65')
Output : "45" is greater than "29" "abc" is greater than "abb" "ABC" is less than "abc" Both are equal "62" is less than 65
Comparing strings : is vs == operator :
Python has the two comparison operator ==
and is
. At first sight they seem to be the same, but actually they are not.
==
compares two variable based on their actual value but is operator compares two variables based on the object id and returns True if the two variables refer to the same object and returns False if two variables refer to the different object.
Sometimes is
operator is also used to compare strings to check if they are equal or not.
Compare contents using is operator :
Example-1
#Program : a = 5 b = 5 if a is b: print("they are same") print('id of "a"',id(a)) print('id of "b"',id(b))
Output : they are same id of "a" 140710783821728 id of "b" 140710783821728
Example 2:
#Program : a = "python" b = "program" if a is b: print("they are same") else: print("they are not same") print('id of "a"',id(a)) print('id of "b"',id(b))
Output: they are not same id of "a" 2104787270768 id of "b" 2104783497712
Compare contents using == operator :
The ==
operator compares the value or equality of two objects.
#Program : a = 5 b = 5 if a == b: print('Both are same')
Output: Both are same
Compare strings using regex in python :
A regular expression(re)Â or regex is a special text string used for describing a search pattern. We can use that to compare strings.
#program : import re repattern = re.compile("88.*") #list of numbers List = ["88.3","88.8","87.1","88.0","28.2"] #check if strings in list matches the regex pattern for n in List: match = repattern.fullmatch(n) if match: print('string',n ,'matched') else: Â print('string',n ,'do not matched')
Output : string 88.3 matched string 88.8 matched string 87.1 do not matched string 88.0 matched string 28.2 do not matched