Case-insensitive string comparison in Python

We generally use Python lists to store items. An online shopping application may contain a list of items in it so that the user can search the item from the list of items. For example, Our shopping application has a list of laptops it sells. List contains many brands and one of them is ‘Lenovo’. If we want to buy a laptop of Lenovo brand we go to the search bar of shopping app and search for ‘Lenovo’. Then it displays all the models of Lenovo laptops. But sometimes the user may type ‘lenovo’ in lowercase or ‘LENOVO’ in upper case. Even then it should display all the models of Lenovo laptops. That means we should perform a case-insensitive check.

 Case-insensitive means the string which you are comparing should exactly be the same as a string which is to be compared but both strings can be either in upper case or lower case. (ie., different cases)

Example 1: Conversion to lower case for comparison

In this example, the user string and each list item are converted into lowercase and then the comparison is made.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# conversion to lowercase for search
  
#function to search item
def check_Laptops():
  
    laptops = ['Msi', 'Lenovo', 'Hp', 'Dell']
  
    your_laptop = 'lenovo'
  
    # 'lenovo' is in lower case but it is present in the list of laptops.
  
    for lapy in laptops:
        
      #convert to lowercase and compare
        if your_laptop.lower() == lapy.lower():
  
            return True
  
    else:
  
        return False
  
# If the function returns true 
if check_Laptops():
  
    print('Laptop is present')
      
  
# If function returns false
else:
  
    print('Laptop is not present')

chevron_right


Output



Laptop is present

Example 2: Conversion to uppercase for comparison

In this example, the user string and each list item are converted into uppercase and then the comparison is made. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# concersion to upper case
  
# Function to search item
def check_Laptops():
  
  laptops = ['Msi', 'Lenovo', 'Hp', 'Dell']
  
  your_laptop = 'HP'
  
  # 'HP' is in upper case but it is
  # present in the list of laptops.
  
  for lapy in laptops:
      
    # convert to uppercase and compare
      if your_laptop.upper() == lapy.upper():
  
          return True
  
  else:
  
      return False
  
  
if check_Laptops():
  
  #If the function is true
  print('Laptop is present')
  
else:
  
  #If the function returns false
  print('Laptop is not present')

chevron_right


Output

Laptop is present

Example 3: 

In this example, the string is not present in the list. So case-insensitive search also returns false.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to search item
def check_Laptops():
  
  laptops = ['Msi', 'Lenovo', 'Hp', 'Dell']
  
  your_laptop = 'Acer'
  
  for lapy in laptops:
      
    #convert to lower and compare
      if your_laptop.lower() == lapy.lower():
          return True
  
  else:
      return False
  
if check_Laptops():
    
  # If the function returns false
  print('Laptop is present')
  
else:
  
  # If the function returns false
  print('Laptop is not present')

chevron_right


Output

Laptop is not present

Example 4:  Comparison using casefold()

The casefold() method works similar to lower() method. But compared to lower() method it performs a strict string comparison by removing all case distinctions present in the string. In German, ‘β‘ is equivalent to “ss“. But every user might not know German, so casefold() method coverts German letter ‘β’ to ‘ss’ whereas we cannot convert German letter ‘β’ to ‘ss’ by using lower() method.

In this example, we are checking whether our classroom is present in the list of classrooms or not.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#initial list
classrooms=['class1','class2','CLASS3','class4','class5']
  
# class to be searched
class_room='claß3'
  
#' claß3' means 'class3'
  
#function to search item
def search_classroom():
   for classes in classrooms:
       if class_room.casefold()==classes.casefold():
           return True
  
   else:
       return False
  
if search_classroom():
    
  # If function returns true
   print('Classroom you are searching is present')
  
else:
    
  # If function returns false
   print('Classroom you are searching is not present')

chevron_right


Output

Classroom you are searching is present

These are the methods in Python for case-insensitive string comparison.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.