Comparing Strings In Python – POFTUT

Comparing Strings In Python


A string is a very useful and popular variable type used in the Python programming language. Sometimes we need to operate two or more strings and compare them. Comparing numbers is easy but the string is a bit different and harder. In this tutorial, we will look at different comparison operations on Python strings.

Find Bigger String

Two numbers can be compared in order to find the bigger one but how can we find a bigger string. Strings are consist of multiple characters and evaluated according to these multiple characters. Comparing two string means comparing the ASCII values of each character one by one. For example:

a='abc'

b='abd'

b > a
Find Bigger String
Find Bigger String

These two strings are very similar but the third character is different for both. If we compare these two strings b will be bigger than a the variable because of d ASCII value is bigger than c ASCII value. And as a result, a boolean value True is returned.

Check If Same with == Operator

The most popular comparison operation is checking the two strings if they are the same. We will use == operator in order to check equality.

a='abc' 
b='abd' 
a == b 
#False 
a = 'abd' 
a == b    
#True
Check If Same
Check If Same

Check If Same with is Operator

Python programming language also provides is operator to check given objects are the same. We can use is operator in order to check if given two string is the same which means two string provides the same characters in the same order.

a='ismail'
b='poftut'
c='ismail'

a is b
#False

b is a
#False

a is c
#True

c is a
#True
Check If Same with is Operator
Check If Same with is Operator

Check If Same with is Operator

Check If Different with != Operator

The reverse of checking equality is checking if two strings are different. If the strings are different from each other this will return boolean True . We will use != operator for these operations.

a = 'abd' 
b = 'abd' 
a != b 
#False 
b = 'abc' 
a != b    
#True
Check If Different
Check If Different

Check If String Empty

Strings can provide different characters but also can contain no value or just spaces. We can check if a string is empty or not by using strip function. We will strip spaces from the string and check equality with an empty string literal like below.

a = 'abc' 
b = ' ' 
b.strip() == '' 
#True 
a.strip() == ''  
#False
Check If String Empty
Check If String Empty

We can also use the string with the if keyword. If the string has a value different than space or empty the if will get a True boolean value.

a = 'abc'       
b = ' '         
if b: 
   print("Empty") 
 
#Empty 

if a: 
   print("Not empty") 
 
#Not empty
Check If String Empty
Check If String Empty

Compare String Case Insensitive

In some cases, we may have strings those need to be compared in a case insensitive manner. We can use some function provided by the Python string type like lower() and upper(). In this example, we will lower all provided strings and compare them with == and != operators.

a='isMail'
b='Poftut'
c='iSmaiL'

a.lower() == b.lower()
#False

a.lower() != b.lower()
#True

a.lower() != c.lower()
#False

a.lower() == c.lower()
#True

b.lower() == c.lower()
#False
Compare String Case Insensitive
Compare String Case Insensitive

LEARN MORE  What Is String Data Type In JavaScript, Java, Python, C# , PHP, C, C++, PowerShell Programming Languages?

Leave a Comment