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
Ignore case in Python strings - Stack Overflow
What is the easiest way to compare strings in Python, ignoring case? Of course one can do (str1.lower() More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
🌐
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 - string1 = "Straße" # German word with ß string2 = "STRASSE" if string1.casefold() == string2.casefold(): print("The strings are equal, ignoring case.") else: print("The strings are not equal, ignoring case.")
🌐
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?
🌐
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.
🌐
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 - Now let’s see how to compare ... 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.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-do-a-case-insensitive-string-comparison-in-python
How do I do a case-insensitive string comparison in Python?
June 10, 2025 - 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") ... The casefold() method in Python is similar to the lower() method, ...
🌐
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.
🌐
Sling Academy
slingacademy.com › article › python-ways-to-compare-2-strings-ignoring-case-sensitivity
Python: 3 ways to compare 2 strings ignoring case sensitivity - Sling Academy
The strings are euqal. Using the lower() or upper() method is a little bit faster than using casefold() when performing bulk case-insensitive comparisons (e.g., with a loop). However, this approach may not work well with some languages due to language-specific rules for case conversion. The lower() and upper() methods rely on the default case conversion rules in Python, which may not accurately handle certain characters or special cases in different languages.
🌐
Delft Stack
delftstack.com › home › howto › python › case insensitive string comparison in python
How to Compare String Case Insensitive String in Python | Delft Stack
February 2, 2024 - It returns a string with all the characters converted into lower case alphabets. We can convert two strings to the lower case with the lower() method and then compare them case-insensitively.
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.

🌐
Python Examples
pythonexamples.org › python-check-if-strings-are-equal-ignoring-case
Check if Two Strings are Equal Ignoring Case in Python
To check if two strings are equal ignoring case in Python, convert both the strings to lowercase or uppercase, and compare these using Equal-to Comparison Operator.
🌐
YouTube
youtube.com › watch
Python Tips and Tricks: Case-Insensitive String Comparisons Done Right - YouTube
How to perform case-insensitive string comparisons, and how to avoid somecommon Unicode issues.#mathbyteacademy #python #pythontipsCode for this Video=======...
Published: July 11, 2022
🌐
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.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to ignore case in python strings
How to Ignore Case in Python Strings - Be on the Right Side of Change
August 25, 2022 - For example, the following three (3) Strings are not identical. If compared with each other, they would return False. This is because each character in the ASCII Table assigns different numeric values for each key or key combination on the keyboard. This article outlines various ways to ignore the case of Strings.