Let's say that I ask a user for raw input and they said, "This is a message." If that raw input contained the word "message" it would perform an action after that. Could I see how this can be done?
-
2If you try to build a content filter, consider this: the word "classic" has "ass" in it (which, btw. is another term for donkey). This is of course only a sample- – knitti Oct 09 '10 at 21:36
5 Answers
Going based on the comment by @knitti, the problem is that you need to split up the sentence into words first, then check:
term = "message" #term we want to search for
input = raw_input() #read input from user
words = input.split() #split the sentence into individual words
if term in words: #see if one of the words in the sentence is the word we want
do_stuff()
Otherwise if you had the sentence "That one is a classic" and you tried to check if it contained the word "lass", it would return True incorrectly.
Of course, this still isn't perfect because then you might have to worry about things like stripping out punctuation and what not (like , . etc.) because otherwise the sentence "That one is a classic." would still return False for a search for "classic" (because of the period at the end). Rather than reinvent the wheel, here's a good post on stripping punctuation from a sentence in Python:
Best way to strip punctuation from a string
There's case-sensitivity to consider too, so you might want to change the raw_input
result and your search term to lowercase before doing a search. You could easily do that by just using the lower()
function on the str
class.
These problems always seem so simple...

- 19,075
- 7
- 52
- 56
-
This doesn't look to work for 2-tokens words! If your word is only one token, yes! But if you are looking for a word like "hot tea" in a sentence, the split() function is not fine! – Reihan_amn Feb 26 '20 at 00:36
This is of course a very simple example:
if "message" in raw_input():
action()
If you are required to map different words to different actions, then you could do something like this:
# actions
def action():
print "action"
def other_action():
print "other action"
def default_action():
print "default action"
# word to action translation function
def word_to_action(word):
return {
"message": action,
"sentence": other_action
}.get(word, default_action)()
# get input, split into single words
w = raw_input("Input: ").split()
# apply the word to action translation to every word and act accordingly
map(word_to_action, w)
Note, that this also defines a default action for the case when the input does not contain any of the trigger words.
See here for more details on the above mapping idiom, which is actually Python's way of achieving 'switch statement'.
def findDog(st):
return 'dog' in st.lower().split()
findDog('Is there a dog here?')

- 57,834
- 11
- 73
- 112

- 11
- 1
-
2Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Jul 31 '22 at 12:44
-
Just write :
term = what you want to be checked in input or something y = input function # what you want to be inputted note: taking y as input is not necessary You can take whatever you want!
then write
if term in y: # checks whether y(input) contain the word you wanted to be checked
what you want to do
see it's just that simple
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 03 '23 at 13:21
if "message" in sentence:
do_action()

- 364,293
- 75
- 561
- 662
-
2
-
3this doesn't check the word! It checks the substring matching! For instance is you are looking for a word like "cheese" and your sentence has "I like cheesecake" this approach shows as one matching! – Reihan_amn Feb 26 '20 at 00:34