0

How can i ensure a particular word in user input in python

For ex var=input("Say Something:") // Input was "I love apples" how to ensure that user input has "apples" in var ??

15th Jun 2017, 9:39 AM
Riya
Riya - avatar
3 Respuestas
+ 4
And if you want even more accurracy, use the regex module: import re if re.search(r"\bapples\b", input("Say something")) is not None: print("You've typed the word apples in your sentence.") RegExp is a powerful language tool for text handling: https://docs.python.org/3/howto/regex.html
15th Jun 2017, 2:34 PM
visph
visph - avatar
0
if "apples" in var: //Do aomething I would first make var all upper or lower case letters with upper() or lower()... That would make Apples, APPLES, apples, ApPLe's, etc all work when you check them
15th Jun 2017, 9:53 AM
LordHill
LordHill - avatar
0
If you want to make sure it's a whole word (if "apples" in var: would also match "applesauce"), try this: if "apples" in var.split(): //
15th Jun 2017, 10:53 AM
Bogdan Sass
Bogdan Sass - avatar