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
827

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
738

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)
🌐
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?
To perform string comparison in Python, we have several built-in methods such as lower(), upper(), and casefold(), which help us to normalize the casing of strings before comparing them. Each method is used for a specific functionality, and we need to choose the right one depending on whether ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › case-insensitive-string-comparison-in-python
Case-insensitive string comparison in Python - GeeksforGeeks
July 15, 2025 - Explanation: This code converts ... set. If the set has only one unique element, it prints "equal" otherwise, "unequal". casefold() method in Python ......
🌐
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.
🌐
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
In Python, when you compare two strings using the equality operator (==), the comparison is case-sensitive by default. This means "Hello" and "hello" are considered different strings.
🌐
Mathspp
mathspp.com › blog › how-to-work-with-case-insensitive-strings
How to work with case-insensitive strings | mathspp
This is a short and practical tutorial that guides you on how to work with case-insensitive strings in Python and teaches how to use the str.lower,...
🌐
LearnPython.com
learnpython.com › blog › python-case-sensitive
Is Python Case-Sensitive? | LearnPython.com
There are times when it would be ... case-insensitive. Imagine a situation when customers are searching for a certain product in an online store. Let’s say they are interested in Finnish designs and look for Alvar Aalto’s vase. What do they type in the search box? Maybe: “Alvar Aalto vase”, but most probably “alvar aalto vase”. Either way, they need to return the same search results. We need to consider case sensitivity in Python when comparing strings...
Find elsewhere
🌐
iO Flood
ioflood.com › blog › using-python-to-compare-strings-methods-and-tips
Python String Comparison Methods | Quick User Guide
August 13, 2024 - Today’s article will explain how to compare strings in Python, providing practical examples to assist our customers utilizing their cloud server hosting services for Python Programming. This guide will take you through the fascinating world of string comparison in Python. We’ll explore a variety of techniques, from case-sensitive and case-insensitive comparisons to comparing permutations of a string and even fuzzy matching.
🌐
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
July 15, 2020 - Exactly what we want for case-insensitive string comparison. One should not use str.casefold() if you are aiming for clean spellings. str.upper().lower() might yield a more printable result: >>> k8s_odd.upper().lower() 'κυβερνήτης' But for case-insensitive comparison that respects a wide range of human languages, str.casefold() is our friend. Python docs on str.casefold ·
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):

Copy#!/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:

Copy235886 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:

CopyPython 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.

🌐
Squash
squash.io › string-comparison-in-python-best-practices-and-techniques
String Comparison in Python: Best Practices and Techniques
If you want to perform case-insensitive string comparison, you can convert the strings to lowercase or uppercase using the lower() or upper() string methods before performing the comparison.
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-compare-two-strings-by-ignoring-case
Python Program to compare two strings by ignoring case
Then we compare both strings using the "==" operator. Since the two strings are identical the output of the code will be "The strings are equal, ignoring case".
🌐
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 - The casefold() method returns a string variable in which all the characters are aggressively converted into lowercase. This new string variable can then be compared to carry out a case insensitive comparison.
🌐
Sololearn
sololearn.com › en › Discuss › 1254102 › how-to-make-python-case-insensitive
How to make python case insensitive | Sololearn: Learn to code for FREE!
First watch this Video: https://www.bing.com/videos/search?q=how+to+make+JUMP_LINK__&&__python__&&__JUMP_LINK+case+insensitive&qpvt=how+to+make+python+case+insensitive&view=detail&mid=59BEBF5235B1BD7AA73C59BEBF5235B1BD7AA73C&&FORM=VRDGAR Second, 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)" SOURCE: https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison · 3rd May 2018, 7:19 AM · Baraa AB · + 19 · GodOfPC 611 most welcome bro 💙 Good luck 😉💚 · 3rd May 2018, 12:21 PM · Baraa AB · + 8 · Rahul George : since there exists a challenge for it: casefold() as a further opportunity ·
🌐
Cherry Servers
cherryservers.com › home › blog › cloud computing › how to do string comparison in python [with examples]
How to do String Comparison in Python | Cherry Servers
November 7, 2025 - However, if you want the string comparison to be case-insensitive, use the lower() method with the strings you are comparing.
🌐
Reddit
reddit.com › r/learnprogramming › need help with case insensitive list comparison in python
r/learnprogramming on Reddit: Need help with case insensitive list comparison in Python
August 26, 2019 -

Hi all,

I'm learning python and have been trying to figure out how to properly compare lists for case insensitivity. If I have two lists, where the first list contains the current user names and the second list is a list of new usernames, how do I get to make sure that if a new user name John won't conflict with a username in the current users of JoHn or JOHN and vice versa?

I have this so far:

current_users = ['John', 'BiLl', 'simcitizzon', 'mIke', 'cHarlie', 'admin']

new_users = ['john', 'simcitiZzon', 'ralphwiggum', 'cherrymcsperry', 'sweettooth347']

for user in new_users:
    if user in current_users:
	    print("Sorry, " + user + " is taken.")
    else:
	    print(user + ", this username is available")
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-case-insensitive-string-replacement
Case insensitive string replacement in Python - GeeksforGeeks
July 23, 2025 - Explanation: re.sub(r"(?i)best", lambda m: "good", a) uses a case-insensitive regex ((?i) inline flag) to match the word "best" in any casing within the string a, and replaces each match using a lambda function that returns "good".
🌐
W3docs
w3docs.com › python
How do I do a case-insensitive string comparison?
string1 = "Hello World" string2 = "HELLO WORLD" if string1.upper() == string2.upper(): print("The strings are case-insensitively equal.") else: print("The strings are not case-insensitively equal.") ... In this example, the comparison will be true and the message "The strings are case-insensitively equal." will be printed.