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 ??
3 Réponses
+ 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
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
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():
//