Assuming ASCII strings:

string1 = 'Hello'
string2 = 'hello'

if string1.lower() == string2.lower():
    print("The strings are the same (case insensitive)")
else:
    print("The strings are NOT the same (case insensitive)")

As of Python 3.3, casefold() is a better alternative:

string1 = 'Hello'
string2 = 'hello'

if string1.casefold() == string2.casefold():
    print("The strings are the same (case insensitive)")
else:
    print("The strings are NOT the same (case insensitive)")

If you want a more comprehensive solution that handles more complex unicode comparisons, see other answers.

Answer from Harley Holcombe on Stack Overflow
Top answer
1 of 15
828

Assuming ASCII strings:

string1 = 'Hello'
string2 = 'hello'

if string1.lower() == string2.lower():
    print("The strings are the same (case insensitive)")
else:
    print("The strings are NOT the same (case insensitive)")

As of Python 3.3, casefold() is a better alternative:

string1 = 'Hello'
string2 = 'hello'

if string1.casefold() == string2.casefold():
    print("The strings are the same (case insensitive)")
else:
    print("The strings are NOT the same (case insensitive)")

If you want a more comprehensive solution that handles more complex unicode comparisons, see other answers.

2 of 15
741

Comparing strings in a case insensitive way seems trivial, but it's not. I will be using Python 3, since Python 2 is underdeveloped here.

The first thing to note is that case-removing conversions in Unicode aren't trivial. There is text for which text.lower() != text.upper().lower(), such as "ß":

>>> "ß".lower()
'ß'
>>> "ß".upper().lower()
'ss'

But let's say you wanted to caselessly compare "BUSSE" and "Buße". Heck, you probably also want to compare "BUSSE" and "BUẞE" equal - that's the newer capital form. The recommended way is to use casefold:

str.casefold()

Return a casefolded copy of the string. Casefolded strings may be used for caseless matching.

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. [...]

Do not just use lower. If casefold is not available, doing .upper().lower() helps (but only somewhat).

Then you should consider accents. If your font renderer is good, you probably think "ê" == "ê" - but it doesn't:

>>> "ê" == "ê"
False

This is because the accent on the latter is a combining character.

>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']

The simplest way to deal with this is unicodedata.normalize. You probably want to use NFKD normalization, but feel free to check the documentation. Then one does

>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True

To finish up, here this is expressed in functions:

import unicodedata

def normalize_caseless(text):
    return unicodedata.normalize("NFKD", text.casefold())

def caseless_equal(left, right):
    return normalize_caseless(left) == normalize_caseless(right)
🌐
GeeksforGeeks
geeksforgeeks.org › python › case-insensitive-string-comparison-in-python
Case-insensitive string comparison in Python - GeeksforGeeks
July 15, 2025 - It converts all strings, checks uniqueness using a set and prints "equal" if all are identical otherwise, "unequal". re.match() checks if a string matches a pattern from the start and with the re.IGNORECASE flag, it ignores case differences.
Discussions

How can I perform case-insensitive string comparison in Python? - Python - Data Science Dojo Discussions
I have two strings, and I want to compare them without considering their cases. How can I achieve this in Python? I have tried using the == operator, but it considers the case while comparing the strings. Here’s what I have done so far: This code snippet uses the lower() method to convert ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
1
0
April 26, 2023
Case sensitive string comparison
As far as I know, it is case sensitive by default. May be you mean case insensitive? Can you give an example and the code you tried? More on reddit.com
🌐 r/learnpython
6
2
January 22, 2021
python - Ignore case in string comparison - Stack Overflow
If I have two variables, a and b and they could be integers, float, or strings. I want to return True if they are equal (in case of string, ignore case). As Pythonic as possible. More on stackoverflow.com
🌐 stackoverflow.com
Ignore case in Python strings - Stack Overflow
The problem is any Python-based ... module string). Could not find anything like that, hence the question here. (Hope this clarifies the question). ... your assumptions are wrong. list.sort() with a key= does not mean "two new allocations per comparison". (list.sort with the cmp=, on the other hand does call the argument for each comparison) ... attempted to rename the question from Ignore case in python strings to ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
LabEx
labex.io › tutorials › python-how-to-compare-two-python-strings-for-equality-in-a-case-insensitive-manner-395043
How to compare two Python strings for equality in a case-insensitive manner? | LabEx
For more advanced scenarios, you can use regular expressions with the re module and the IGNORECASE flag: Add the following code to your case_insensitive.py file: ## Case-insensitive comparison using regular expressions import re text = "Python is a great programming language." pattern1 = "python" ## Check if 'python' exists in the text (case-insensitive) match = re.search(pattern1, text, re.IGNORECASE) print(f"Found '{pattern1}' in text?
🌐
TutorialsPoint
tutorialspoint.com › article › python-program-to-compare-two-strings-by-ignoring-case
Python Program to compare two strings by ignoring case
March 27, 2026 - Use lower() or upper() for basic case-insensitive string comparisons. For international text with special Unicode characters, casefold() provides the most reliable results.
🌐
Know Program
knowprogram.com › home › python compare strings ignore-case
Python Compare Strings Ignore-case - Know Program
November 17, 2021 - Python compare strings ignore case | casefold() method removing all case distinctions present in a string. It ignores cases when comparing.
🌐
TutorialsPoint
tutorialspoint.com › article › how-do-i-do-a-case-insensitive-string-comparison-in-python
How do I do a case-insensitive string comparison in Python?
March 24, 2026 - 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 ...
🌐
thisPointer
thispointer.com › home › python › python : how to compare strings ? | ignore case | regex | is vs == operator
Python : How to Compare Strings ? | Ignore case | regex | is vs == operator - thisPointer
January 11, 2022 - As both the strings has similar characters but in different case. So to match these strings by ignoring case we need to convert both strings to lower case and then match using operator == i.e.
Find elsewhere
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How can I perform case-insensitive string comparison in Python? - Python - Data Science Dojo Discussions
April 26, 2023 - I have tried using the == operator, but it considers the case while comparing the strings. Here’s what I have done so far: This code snippet uses the lower() method to convert both strings to lowercase before comparing them using the == operator.
🌐
Mathspp
mathspp.com › blog › how-to-work-with-case-insensitive-strings
How to work with case-insensitive strings | mathspp
January 21, 2023 - The method str.casefold is the method that you want to use when you need to do caseless, or case-insensitive, comparisons in Python.
🌐
Delft Stack
delftstack.com › home › howto › python › case insensitive string comparison in python
Case Insensitive String Comparison in Python
February 2, 2024 - There are three main methods used to carry out case insensitive string comparison in python which are lower(), upper() and casefold().
🌐
LearnPython.com
learnpython.com › blog › python-case-sensitive
Is Python Case-Sensitive? | LearnPython.com
Using the casefold() method is the strongest and the most aggressive approach to string comparison in Python. It’s similar to lower(), but it removes all case distinctions in strings.
Top answer
1 of 16
74

Here is a benchmark showing that using str.lower is faster than the accepted answer's proposed method (libc.strcasecmp):

#!/usr/bin/env python2.7
import random
import timeit

from ctypes import *
libc = CDLL('libc.dylib') # change to 'libc.so.6' on linux

with open('/usr/share/dict/words', 'r') as wordlist:
    words = wordlist.read().splitlines()
random.shuffle(words)
print '%i words in list' % len(words)

setup = 'from __main__ import words, libc; gc.enable()'
stmts = [
    ('simple sort', 'sorted(words)'),
    ('sort with key=str.lower', 'sorted(words, key=str.lower)'),
    ('sort with cmp=libc.strcasecmp', 'sorted(words, cmp=libc.strcasecmp)'),
]

for (comment, stmt) in stmts:
    t = timeit.Timer(stmt=stmt, setup=setup)
    print '%s: %.2f msec/pass' % (comment, (1000*t.timeit(10)/10))

typical times on my machine:

235886 words in list
simple sort: 483.59 msec/pass
sort with key=str.lower: 1064.70 msec/pass
sort with cmp=libc.strcasecmp: 5487.86 msec/pass

So, the version with str.lower is not only the fastest by far, but also the most portable and pythonic of all the proposed solutions here. I have not profiled memory usage, but the original poster has still not given a compelling reason to worry about it. Also, who says that a call into the libc module doesn't duplicate any strings?

NB: The lower() string method also has the advantage of being locale-dependent. Something you will probably not be getting right when writing your own "optimised" solution. Even so, due to bugs and missing features in Python, this kind of comparison may give you wrong results in a unicode context.

2 of 16
7

Your question implies that you don't need Unicode. Try the following code snippet; if it works for you, you're done:

Python 2.5.2 (r252:60911, Aug 22 2008, 02:34:17)
[GCC 4.3.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_COLLATE, "en_US")
'en_US'
>>> sorted("ABCabc", key=locale.strxfrm)
['a', 'A', 'b', 'B', 'c', 'C']
>>> sorted("ABCabc", cmp=locale.strcoll)
['a', 'A', 'b', 'B', 'c', 'C']

Clarification: in case it is not obvious at first sight, locale.strcoll seems to be the function you need, avoiding the str.lower or locale.strxfrm "duplicate" strings.

🌐
iO Flood
ioflood.com › blog › using-python-to-compare-strings-methods-and-tips
Python String Comparison Methods | Quick User Guide
August 13, 2024 - Because the strings are identical, they are stored in the same memory location and you just have to check that two strings point to the same memory. Case insensitive comparisons require additional processing. What if you want to disregard case when comparing strings? Python offers several methods for this purpose, including ‘lower()’, ‘upper()’, and ‘casefold()’.
🌐
DEV Community
dev.to › bowmanjd › case-insensitive-string-comparison-in-python-using-casefold-not-lower-5fpi
Case-insensitive string comparison in Python using casefold, not lower - DEV Community
February 9, 2026 - Here is a discipline I am trying to adopt in my Python programs: use "My string".casefold() instead of "My string".lower() when comparing strings irrespective of case.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python › python compare strings (==, ignore case, substring, examples)
Python Compare Strings (==, Ignore Case, Substring, Examples) | GoLinuxCloud
April 12, 2026 - Yes, Python string comparison is case-sensitive by default. ... To ignore case, always normalize strings using lower() or casefold().