0

I have created a library and a search interface. It will return results only if the first letter is uppercase as all my data starts with upper case letters. Is there a way of searching but removing the case sensitivity. I need it to not alter the data when it returns it.

The only method i can think of using is to change the first letter to uppercase but it seems really messy.

Thanks

UPDATE: Simplified version of what i'm trying to do

data = 'The Works of William Shakespeare'
key = 'The'
if key in data:
    print "Match Found"
Match Found

key = 'the'
if key in data:
    print "Match Found"

The key wasn't found in the second attempt. Is there a way of automatically changing the case of the data string and the case of the input key?

Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30

2 Answers2

0

Keep a separate field for the lower-cased version of the text you're searching, and then compare the lower-cased version of the search query.

Amber
  • 507,862
  • 82
  • 626
  • 550
0

Normalize the key that you are searching for. So if its in lower case, convert it to upper case (or in your case, initial caps), by using title():

key = 'the'
if key.title() in 'The Haystack I Need to Search':
    print('Found')
else:
    print('Did not find {}'.format(key))
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284