HOWTO · Python

How to Compare Strings Without Case Sensitivity in Python

There are three main methods used to carry out case insensitive string comparison in python which are lower(), upper() and casefold().

On this page

To compare Python strings without regard to letter case, normalize both strings with the same method and compare the results. Use lower() or upper() for straightforward comparisons; choose casefold() when Unicode caseless matching matters, such as treating ß and ss as equivalent.

Case-Insensitive String Comparison With lower()

The built-in lower() method returns a lowercase copy of a string. Calling it on both values before == makes strings such as Hello World! and HELLO WORLD! compare equal. The method handles ordinary case conversion, but it does not apply all Unicode case-folding rules. Normalize both operands the same way; changing only one side can still make an otherwise matching pair compare unequal. The returned value is a new string, so the original spelling remains available for display or storage.

This self-contained example compares two differently cased values and prints the Boolean result.

first = "Hello World!"
second = "HELLO WORLD!"

print(first.lower() == second.lower())
True

For input that should match, the comparison evaluates to True. Keep the original strings if their spelling or display case matters; lower() returns a new string and does not change either variable.

Case-Insensitive String Comparison With upper()

You can use the built-in upper() method in the same way. It returns an uppercase copy, so normalize both values with upper() before comparing them. This is an alternative to lower(); choose one normalization consistently for both sides. The important part is applying the same method to each string, rather than mixing lowercase conversion on one operand with uppercase conversion on the other.

The example compares the same words after converting each to uppercase.

first = "Hello World!"
second = "hello world!"

print(first.upper() == second.upper())
True

Both approaches are convenient when the strings use ordinary letter-case variants. They do not make every Unicode spelling equivalent, so use casefold() when the comparison needs Unicode-aware caseless matching.

Case-Insensitive String Comparison With casefold()

The casefold() method is designed for caseless matching. It can map characters beyond the lowercase conversion performed by lower(). For example, lower() leaves the German letter ß unchanged, while casefold() maps it to ss. As a result, the case-folded forms of "straße" and "STRASSE" compare equal. This is useful when matching Unicode text whose equivalent spellings do not differ only by ASCII letter case.

Use casefold() on both values when Unicode case-folding behavior is important, then compare the returned strings with ==. The following runnable example illustrates the ß and ss comparison.

first = "straße"
second = "STRASSE"

print(first.casefold() == second.casefold())
True

Prefer casefold() for general Unicode caseless matching. For simpler text where ordinary case conversion is sufficient, lower() or upper() can make the intent clear. These methods normalize values for comparison; they do not replace the original strings.