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
Case sensitive string comparison
python - pandas "case insensitive" in a string or "case ignore" - Stack Overflow
How do you check if a string contains a substring in Python?
How can I check if a string contains a substring in Python?
1. Use the in keyword, which returns True if the substring is present within the string.
2. Use str.find(), which returns the index of the first occurrence of the substring or -1 if not found.
3. Use str.index(), which raises an error if the substring is not found.
4. Use startswith() and endswith() to check if a string begins or ends with a specific substring.
How do you check if a string contains a substring case-insensitively?
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.
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)So I have a script which replaces a set of strings in a text file, but it needs to be case sensitive, is this a built in function or do I need to do some black magic
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)]