1

So guys i'am trying to make a dictionary app. I want it to be case-insensitive. Firstly i saw some solutions for this promblem but non of them suited with me. Let me explain it with an example: Let's say i have the word School my code works fine when i search it like School but it doesn't work when i search it like school.

i really didn't get this solution https://stackoverflow.com/a/15400311/9692934

    key_to_search = input() #not raw_input() since its python 3    
    with open("fileOfWords.txt") as enter:
        for line in enter:
            if line.startswith("%s" % key_to_search):
                print(key_to_search + " is in the dictionary")

I want School to be equal to school and scHool and schooL. But in my case School is just equaled to School

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • So it's not the casing of the *filename* you're asking about, but about how to compare strings case-insensitive? Then your title is very misleading as `open` have nothing to do with it. Please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 12 '19 at 20:50
  • @Someprogrammerdude i tried to ask that is there a way to only lower or upper a letter in a word. If the answer is no i am going to lower all – Ahmet Ertuğrul Kaya Jan 12 '19 at 21:15

2 Answers2

0

If you want to be case-insensitive, you can just convert the input and line into lowercase and compare them.

key_to_search = input() #not raw_input() since its python 3    
with open("fileOfWords.txt") as enter:
    for line in enter:
        if line.lower().startswith(key_to_search.lower()):
            print(key_to_search + " is in the dictionary")
Nic Laforge
  • 1,776
  • 1
  • 8
  • 14
0

I think you can simply call lower upon your search string: key_to_search = key_to_search.lower() and save all words in lower case. Your code will be:

key_to_search = input().lower() #not raw_input() since its python 3    
    with open("fileOfWords.txt") as enter:
        for line in enter:
            if line.startswith("%s" % key_to_search):
                print(key_to_search + " is in the dictionary")
Masked Man
  • 2,176
  • 2
  • 22
  • 41