If you don't want to use str.lower(), you can use a regular expression:
import re
if re.search('mandy', 'Mandy Pande', re.IGNORECASE):
# Is True
Answer from eumiro on Stack OverflowIf you don't want to use str.lower(), you can use a regular expression:
import re
if re.search('mandy', 'Mandy Pande', re.IGNORECASE):
# Is True
You are looking for the .lower() method:
string1 = "hi"
string2 = "HI"
if string1.lower() == string2.lower():
print("Equals!")
else:
print("Different!")
Btw, There's another post here. Try looking at this.
Perform Case Insentive Search
help with IGNORECASE
Trying to create case-insensitive user search for list, but '.lower()' method isn't working, any help please?
Check if case sensitive with python (with boolean)
I have simple find loop but I want it to ignore case and I just cant get it to .
import re
names = ['Tilt back', 'speed', 'gist']
for name in names:
if name.startswith('tilt', re.IGNORECASE):
print(name)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.
How do I count how many times a case sensitive word appears somewhere using a boolean.
So for example:
word = input("Search this word: ")Then I want to check if case sensitive == True or False. If it's true, it will only find the exact same words.
Also, the word can't be a part of another word. So it may not find "Apple" in Applepie. But it can only find "Apple" if it's independent like this: Apple Pie
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.
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)