Case-insensitive string comparison in Python
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.
a = ["hello", "HELLO", "HeLLo", "world"]
b = [s.lower() for s in a] # Lowercase all
if len(set(b)) == 1:
print("equal")
else:
print("unequal")
Output
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.
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")
Output
equal
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.
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")
Output
equal
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".