0

This is the code i have so far but i want it to treat the lower case and upper case words the same not sure how to though any ideas?(E.g. CASE, case and CAse the same).


sentence= input("Enter a sentence")
keyword= input("Input a keyword from the sentence")
words = sentence.split(' ')

for i, word in enumerate(words):`enter code here`
    if keyword == word:
        print(i+1)

martineau
  • 119,623
  • 25
  • 170
  • 301
HariSolo
  • 19
  • 1
  • 1
  • 6

3 Answers3

3

To compare two words ignoring case, simply convert them both to, e.g., lower case: word1.lower() == word2.lower().

Sam Marinelli
  • 999
  • 1
  • 6
  • 16
2

You can use str.upper() or str.lower() to turn a string into either all uppercase or all lowercase respectively.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Ali Camilletti
  • 116
  • 1
  • 3
  • 11
  • On Python >= 3.3, [str.casefold()](https://docs.python.org/3/library/stdtypes.html#str.casefold) is most appropriate, which is specifically designed to remove case information. – SethMMorton Feb 02 '17 at 20:59
2

to treat the lower case and upper case words the same

Use str.lower() function:

for i, word in enumerate(words):
    if keyword.lower() == word.lower():
        print(i+1)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105