1

So basically my question is this. Let's say I have a list with values

l1st = ['dog', 'cat', 'bird', 'monkey']

And I ask the user to input

input = raw_input('Please enter the word you wish to search for: ')

Here's the big question. Let's say the person wants to search for

dog

but types it in as

Dog

I don't want the program to give him an error message just because he got the capitalization wrong. What is a method I could use for a list.index search to look for, if not the exact word, the closest similar word?

  • https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison-in-python – Dadep Jun 14 '17 at 16:46

2 Answers2

1

You can compare the strings by turning both into their lower case form. Here's the documentation. Just as you're doing the search use:

if input.lower() == item.lower():
    do_stuff()
cookiedough
  • 3,552
  • 2
  • 26
  • 51
1

You can use the .lower() method to take the input and create the lower case version. You can also look at this link https://docs.python.org/2/library/difflib.html#difflib.get_close_matches which will get closest match to a word froma. List. Hope this helps

cbolles
  • 475
  • 5
  • 17