Case-insensitive string comparison in Python
Last Updated :
22 Apr, 2025
The goal here is to compare strings in a list without considering their case, meaning the comparison should ignore whether letters are uppercase or lowercase. For example, given the list ["hello", "HELLO", "HeLLo"], we want to check if all strings are equal, ignoring the case. Let's explore different approaches to accomplish this.
Using lower() and set()
We can convert all strings in a list to the same case (like lowercase) and then check if they are the same. This is a quick and easy way to compare strings without considering letter case.
Python
a = ["hello", "HELLO", "HeLLo", "world"]
b = [s.lower() for s in a] # Lowercase all
if len(set(b)) == 1:
print("equal")
else:
print("unequal")
Explanation: This code converts all strings in the list to lowercase for case-insensitive comparison, stores them in a new list b and checks if all elements are identical by converting the list to a set. If the set has only one unique element, it prints "equal" otherwise, "unequal".
Using casefold()
casefold() method in Python performs a case-insensitive comparison that accounts for locale-specific character differences.
Python
a = ["straße", "STRASSE", "strasse", "Straße"]
c= [s.casefold() for s in a] # Casefold all strings
if len(set(c)) == 1:
print("equal")
else:
print("unequal")
Explanation: This code uses casefold() for accurate case-insensitive comparison, especially with international characters. It converts all strings, checks uniqueness using a set and prints "equal" if all are identical otherwise, "unequal".
Using re.match
re.match() checks if a string matches a pattern from the start and with the re.IGNORECASE flag, it ignores case differences. However, it's slower than simpler string comparison methods due to extra processing.
Python
import re
a = ["hello", "HELLO", "HeLLo"]
res= [re.match(s, a[0], re.IGNORECASE) for s in a] # Case-insensitive match
if all(res):
print("equal")
else:
print("unequal")
Explanation: This code performs a case-insensitive match of each string with the first one using re.match() and checks if all matches succeed. It prints "equal" if they do otherwise, "unequal".
Similar Reads
Python - Case Insensitive Strings Grouping Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like first letter or any other factor. These type of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by c
4 min read
Case insensitive string replacement in Python We are given a string and our task is to replace a specific word in it, ignoring the case of the letters. This means if we want to replace the word "best" with "good", then all variations like "BeSt", "BEST", or "Best" should also be replaced. For example, if the input is "gfg is BeSt", then the out
3 min read
Python | Convert string list into multiple cases Sometimes, while working with Python strings, we might have a problem in which we have list of strings and we wish to convert them into specified cases. This problem generally occurs in the cases in which the strings we receive are ill-cased. Let's discuss certain ways in which this task can be perf
4 min read
Python | Case Counter in String Sometimes, while working with Python String, we can have a problem in which we need to separate the lower and upper case count. This kind of operation can have its application in many domains. Let's discuss certain ways in which this task can be done. Method #1: Using map() + sum() + isupper() + isl
7 min read
Python - Specific case change in String List While working with String lists, the problem of cases is common, but sometimes, we are concerned about changing cases in strings selectively. i.e. on the basis of another list. This can have applications in day-day programming. Let us discuss certain ways in which this task can be performed. Method
7 min read
Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:Using Comparison Operator(==)The si
2 min read