Case Insensitive String Comparison in Python



In Python, string comparisons are case-sensitive by default. For example, when we consider the strings "Hello" and "hello" then they are considered as two different strings because of the difference in their letter casing.

 Case-insensitive String Comparison in Python

In some tasks, such as searching user input, comparing filenames, or processing natural language, it's important to compare strings without considering case differences.

  • To perform string comparison in Python, we have several built-in methods such as lower(), upper(), and casefold(), which help us to normalize the casing of strings before comparing them.
  • Each method is used for a specific functionality, and we need to choose the right one depending on whether we're working with basic ASCII text or Unicode strings.

In this article, we are going to explore different methods to perform case-insensitive string comparisons in Python.

Using lower() Method

The lower() method in Python converts all characters in a string to lowercase. This method does not modify the original string, as strings in Python are immutable, but returns a new string with all characters converted to lowercase.

str1 = "Tutorialspoint"
str2 = "tutorialspoint"

if str1.lower() == str2.lower():
   print("The strings are equal i.e., case-insensitive")
else:
   print("The strings are not equal")

Following is the output of the above program -

The strings are equal i.e., case-insensitive

Using upper() Method

The upper() method in Python converts all characters in a string to uppercase. This method also returns a new string with all characters converted to uppercase.

str1 = "Tutorialspoint"
str2 = "TUTORIALSPOINT"

if str1.upper() == str2.upper():
   print("The strings are equal i.e., case-insensitive")
else:
   print("The strings are not equal")

Here is the output of the above example -

The strings are equal i.e., case-insensitive

Using casefold() Method

The casefold() method in Python is similar to the lower() method, but the difference is caseless matching, which is useful for comparing strings containing special characters or Unicode. This is recommended when working with Unicode data.

str1 = "Straße"
str2 = "strasse"

if str1.casefold() == str2.casefold():
   print("The strings are equal (case-insensitive with casefold)")
else:
   print("The strings are not equal")

Below is the output of the above program -

The strings are equal (case-insensitive with casefold)

Using locale.strcoll() Method

The locale.strcoll() method of the locale module in Python is used for locale-aware string comparison. This method is useful when we need to compare strings according to the rules of a specific locale, which may affect case sensitivity and sorting order.

Before using strcoll(), we need to set the appropriate locale using locale.setlocale(), then the comparison will be done in a case-insensitive way by converting strings to lowercase or uppercase before passing to the strcoll() method.

import locale

# Set locale to US English
locale.setlocale(locale.LC_COLLATE, 'en_US.UTF-8')

str1 = "Python"
str2 = "python"

if locale.strcoll(str1.lower(), str2.lower()) == 0:
   print("The strings are equal (locale-aware case-insensitive comparison)")
else:
   print("The strings are not equal")

Following is the output of the above program -

The strings are equal (locale-aware case-insensitive comparison)
Updated on: 2025-06-10T16:37:19+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements