username = 'MICHAEL89'
if username.upper() in (name.upper() for name in USERNAMES):
    ...

Alternatively:

if username.upper() in map(str.upper, USERNAMES):
    ...

Or, yes, you can make a custom method.

Answer from nmichaels on Stack Overflow
🌐
Bobby Hadz
bobbyhadz.com › blog › python-check-if-string-is-in-list-case-insensitive
Check if List contains a String case-insensitive in Python | bobbyhadz
Copied!my_list = ['BOBBY', 'HADZ', ... member of l, otherwise it evaluates to False. Both strings have to either be lowercase or uppercase to perform a case-insensitive comparison....
🌐
GeeksforGeeks
geeksforgeeks.org › python › case-insensitive-string-comparison-in-python
Case-insensitive string comparison in Python - GeeksforGeeks
July 15, 2025 - If the set has only one unique element, it prints "equal" otherwise, "unequal". casefold() method in Python performs a case-insensitive comparison that accounts for locale-specific character differences.
🌐
Reddit
reddit.com › r/learnpython › trying to create case-insensitive user search for list, but '.lower()' method isn't working, any help please?
r/learnpython on Reddit: Trying to create case-insensitive user search for list, but '.lower()' method isn't working, any help please?
October 30, 2019 -

Here is the relevant section of code as I originally wrote it:

  elif choice == "4":
      print('What would you like to find? ')
      search_item = input()
      if search_item not in thislist:
          print ("Not found in this list:")
      else:
          print ("Found in this list:")
      print(', '.join(thislist))

...but it turned out that this was case-sensitive to user input, which is not desired, so I tried...

  elif choice == "4":
      print('What would you like to find? ')
      search_item = input()
      if (search_item.lower() not in thislist.lower()):
          print ("Not found in this list:")
      else:
          print ("Found in this list:")
      print(', '.join(thislist))

...as mentioned, for instance here. But it doesn't work, and I get...

Traceback (most recent call last): File "main.py", line 39, in <module> if (search_item.lower() not in thislist.lower()): AttributeError: 'list' object has no attribute 'lower'

I'm guessing that maybe it's because one or both of these isn't a string, but I'm not sure how to fix that if that is really the problem. Any help please? TIA.

BTW, I'm using https://repl.it, as I don't have a Python IDE installed on my home PC.

🌐
EyeHunts
tutorial.eyehunts.com › home › python string contains case insensitive | example code
Python string contains case insensitive | Example code
April 25, 2022 - list1 = ["Apple", "Lenovo", "HP", "Samsung", "ASUS"] s = "lenovo" s_lower = s.lower() res = s_lower in (string.lower() for string in list1) print(res) ... Convert strings into lower or upper case. This is if you’re doing the exact comparison. str1 = "Hello" str2 = "HELLO" if str1.lower() == str2.lower(): print('Both Strings are same') else: print('Strings are not same')
🌐
Reddit
reddit.com › r/learnprogramming › python question: comparing lists, case insensitive
r/learnprogramming on Reddit: python question: comparing lists, case insensitive
August 2, 2017 -

Hi everyone, I am just starting to learn how to code, running into a problem when I try to compare two lists, my code is below:

current_users = ['May', 'April', 'Wu', 'Su', 'Lulu'] new_users = ['may', 'mike', 'jones', 'chow', 'paul'] for new_user in new_users: if new_user in current_users: print('username '+new_user+' is not available.') else: print('welcome')

when I try to make the current_users list case insensitive for comparing, I used:

current_users.lower() ==['may', 'april', 'wu', 'su', 'lulu']

when I run the code, result in a violation, stated that list can not have contribute .lower().

Is there anyway to make the list case insensitive?

thanks in advance.

🌐
GeeksforGeeks
geeksforgeeks.org › python-ways-to-sort-list-of-strings-in-case-insensitive-manner
Python - Ways to sort list of strings in case-insensitive manner - GeeksforGeeks
January 17, 2025 - key=lambda x: x.lower() uses a custom lambda function to convert each string to lowercase for case-insensitive sorting. ... We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order ...
🌐
GitHub
github.com › pywbem › nocaselist
GitHub - pywbem/nocaselist: A case-insensitive list for Python · GitHub
Class NocaseList is a case-insensitive list that preserves the lexical case of its items. ... $ python >>> from nocaselist import NocaseList >>> list1 = NocaseList(['Alpha', 'Beta']) >>> print(list1) # Any access is case-preserving ['Alpha', ...
Author: pywbem
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-case-insensitive-string-counter
Python – Case Insensitive string counter | GeeksforGeeks
April 23, 2023 - Loop through the strings in the list. Convert each string to lowercase using the str.lower() method. If the lowercase string is already in the dictionary, increase its value by 1. If the lowercase string is not in the dictionary, add it as a key with a value of 1. Print the dictionary. ... # Strings Frequency (Case Insensitive) # initializing list test_list = ["Gfg", "Best", "best", "gfg", "GFG", "is", "IS", "BEST"] # printing original list print("The original list is : " + str(test_list)) # create an empty dictionary to store the frequency of strings freq_dict = {} # loop through the strings
🌐
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")
🌐
Peterbe.com
peterbe.com › plog › case-insensitive-list-remove-call
Case insensitive list remove call - Peterbe.com
April 10, 2006 - Today I had to fix an issue where I couldn't use somelist.remove(somestring) because the somestring variable might be in there but of a different (case)spelling. Here was the original code:: def ss(s): return s.lower().strip() if ss(name) in names: foo(name + " was already in 'names'") names.remove(name) The problem there is that you get an ValueError if the name variable is "peter" and the names variable is ["Peter"]. Here is my solution. Let me know what you think: def ss(s): return s.lower().strip() def ss_remove(list_, element): correct_element = None element = ss(element) for item in list_: if ss(item) == element: list_.remove(item) break L = list('ABC') L.remove('B') #L.remove('c') # will fail ss_remove(L, 'c') # will work print L # prints ['A']
🌐
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 - This article outlines various ways to ignore the case of Strings. 💬 Question: How would we write code to compare Strings? We can accomplish this task by one of the following options: ... This method uses lower() and a lambda to convert a List of Strings to lower case to search for an Employee.
🌐
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. This is a more efficient way to make case-insensitive comparisons in Python.
🌐
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.
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)
🌐
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 - There are three main methods used to carry out case insensitive string comparison in python which are lower(), upper() and casefold().