+4 votes
in Programming Languages by (74.2k points)
I want to search a string in a list of strings. The strings in the list may be in upper or lower case. How can I do case-insensitive search?

E.g.

aa=['hello','How','Are','you']

If I search 'Hello' or 'HELLO' in the list, it should return True.

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

To do a case-insensitive search, you need to convert your target string and strings in the list to either lowercase or uppercase.

Here is an example:

>>> aa=['hello','How','Are','you']
>>> 'Are'.lower() in [i.lower() for i in aa]
True
>>> 'How'.lower() in [i.lower() for i in aa]
True
>>> 'hoW'.lower() in [i.lower() for i in aa]
True
>>> 'HeLLo'.lower() in [i.lower() for i in aa]
True


...