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
  • 112,593
  • 23
  • 157
  • 280
HariSolo
  • 9
  • 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
  • 959
  • 1
  • 6
  • 13
2

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

Moinuddin Quadri
  • 43,657
  • 11
  • 92
  • 117
Ali Camilletti
  • 96
  • 1
  • 4
  • 10
  • 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
  • 75,918
  • 4
  • 51
  • 91