How to match a substring in a string ignoring case?


I'm looking for ignore case string comparison in Python. I tried with:

if line.find('mandy') >= 0:

but no success for ignore case. I need to find a set of words in a given text file. I am reading the file line by line. Word on a line can be mandy, Mandy, MANDY, etc. (I don't want to use toupper/tolower, etc.).

I'm looking for the Python equivalent of the Perl code below.

if($line=~/^Mandy Pande:/i)
8/5/2018 12:52:07 AM

Accepted Answer

If you don't want to use str.lower(), you can use regexp:

import re

if'mandy', 'Mandy Pande', re.IGNORECASE):
    # is True
3/12/2016 3:11:36 PM

There's another post here. Try looking at this.

BTW, you're looking for the .lower() method:

string1 = "hi"
string2 = "HI"
if string1.lower() == string2.lower():
    print "Equals!"
    print "Different!"

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow