Home > Mobile >  How to integrate the .lower() function into a python script that checks for certain words?
How to integrate the .lower() function into a python script that checks for certain words?

Time:10-05

I have a script that has a list of possible words, and an input, then checks for the words in the input but it only checks for the specific word, and I’m not sure how to integrate .lower() in it.

term = ["test1", "test2"]
raw_input = input()

if all(Variable in raw_input for Variable in term):
    print("Both words were found")
else:
    print("Couldn't find required words")

But I’d like to add a .lower() I've tried but can't find a way. Thanks

CodePudding user response:

I hope this is what you are looking for.

term = ["test1", "test2"]
raw_input = input()

# check if all terms are in the input string including lower and upper case
if all(x.lower() in raw_input.lower() for x in term):
    print("Both words were found")
else:
    print("Couldn't find required words")

CodePudding user response:

I assume you know that your values in term are lowercase because you define it yourself, and that the user-defined input could either be lowercase or uppercase. In this situation you can use

term = [“test1”, “test2”]
raw_input = input()

if (all(Variable in raw_input.lower() for Variable in term)):
 print("Both words were found")
else:
 print("Couldn't find required words")

CodePudding user response:

You can apply the lower anywhere you see fit, but in this case, for performance, I added a new variable:

lower_input = raw_input.lower()
all(word in lower_input for word in terms)

Please notice that I renamed term to terms... Just to make sense in plain English.

  • Related