-1

I know little about Python Language, I would appreciate if You could help me. I was going to write a code which basically searches for any matching words from Specified List(eg. apple, pear, car, house etc) in an entered text.

Thanks in advance James

James
  • 1
  • 1

1 Answers1

0

You can check if a string contains another sub-string using the in operator - see this answer for further examples of this.

# put the words you want to match into a list
matching_words = ["apple", "pear", "car", "house"]

# get some text from the user 
user_string = input("Please enter some text: ")

# loop over each word in matching_words and check if it is a substring of user_string
for word in matching_words:
    if word in user_string:
        print("{} is in the user string".format(word))

Running this gives

>>> Please enter some text: I'm in my house eating an apple
apple is in the user string
house is in the user string
Community
  • 1
  • 1
dannymilsom
  • 2,386
  • 1
  • 18
  • 19
  • No problem and welcome to stack overflow! As an introduction to how the question and answer system works this link is quite helpful http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – dannymilsom Mar 12 '14 at 00:08