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 Overflowusername = '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.
str.casefold is recommended for case-insensitive string matching. @nmichaels's solution can trivially be adapted.
Use either:
if 'MICHAEL89'.casefold() in (name.casefold() for name in USERNAMES):
Or:
if 'MICHAEL89'.casefold() in map(str.casefold, USERNAMES):
As per the docs:
Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase,
lower()would do nothing to 'ß';casefold()converts it to "ss".
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
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.
if 'power' in choice.lower():
should do (assuming choice is a string). This will be true if choice contains the word power. If you want to check for equality, use == instead of in.
Also, if you want to make sure that you match power only as a whole word (and not as a part of horsepower or powerhouse), then use regular expressions:
import re
if re.search(r'\bpower\b', choice, re.I):
This if you're doing exact comparison.
if choice.lower() == "power":
Or this, if you're doing substring comparison.
if "power" in choice.lower():
You also have choice.lower().startswith( "power" ) if that interests you.
Series.str.contains has a case parameter that is True by default. Set it to False to do a case insensitive match.
df2 = df1['company_name'].str.contains("apple", na=False, case=False)
If you want to do the exact match and with strip the search on both sides with case ignore,
df[df['Asset Name'].str.strip().str.match('searchstring'.strip(), case=False)]
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)