0
key_word = ["apple","Apple","Boy","boy"]

title1 = "Where the boy"
title2 = "The Boy playing cricket"
title3 =  "hello world"
title4 = "I want to buy apple vinegar"

I tried this but nothing is print:

if title1 in key_word:
   print(title1)
petezurich
  • 9,280
  • 9
  • 43
  • 57
ByHala
  • 81
  • 6
  • You are currently checking whether `"Where the boy"` is in the list `key_word`, which it is not. [This answer](https://stackoverflow.com/a/6531704) may be helpful for what you are trying to do. – Henry May 06 '22 at 18:45
  • By what logic do you think that it *should* print something? – Scott Hunter May 06 '22 at 18:45
  • Scott Hunter I am new in python and programming language. I just start learning python. – ByHala May 06 '22 at 18:46
  • My question really wasn't about Python, but what you thought you were expressing with it. – Scott Hunter May 06 '22 at 18:57

3 Answers3

3

You have to check if your key word is in the title, and not the other way around.

key_words = ["apple", "boy"]
titles = [
    "Where the boy", 
    "The Boy playing cricket", 
    "hello world", 
    "I want to buy apple vinegar"
]

for title in titles:
    for key_word in key_words:
        if key_word.lower() in title.lower():
            print(title)
            break

For each title, for each keyword, check if the lowercase version of the keyword is in the lowercase version of the title - if it is, print the title and stop going through the keywords (which is what break does).

This will start with I want to buy apple vinegar, then check if apple is in the title - which it is, and then print that. For the next entry, The Boy playing cricket, we check if apple is in the list, which it isn't - so we move on to the next word, boy - which is in the list.

I removed your duplicate entries since lower makes those redundant.

I also changed your titles into a list - instead of having title1, title2, etc. This makes it possible to go through the list without having to specify each variable name, and lets you extend the set of titles being checked later easily.

Another small detail is that this only checks if the letters appear in the same order; it does not check that they constitute a word (so a title containing "apples" would get a match, so would a title containing "tomboy"). You can work around this by splitting your title into words by using .split() first, and then checking if your keyword is in that list.

Another option is using regular expressions, but that is a more advanced technique - you can consider doing that later when you have more experience.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
0
key_word = ["apple","Apple","Boy","boy"]

title1 = "Where the boy"
title2 = "The Boy playing cricket"
title3 =  "hello world"
title4 = "I want to buy apple vinegar"

for word in key_word:
    if word in title1:
        print(title1)

If you want to check all titles, put them in a list:

key_word = ["apple","Apple","Boy","boy"]

titles = [
    "Where the boy",
    "The Boy playing cricket",
    "hello world",
    "I want to buy apple vinegar",
]

for word in key_word:
    for title in titles:
        if word in title:
            print(title)
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • This solution will work for the given data. However, it's not a general solution. Consider what would happen if you add 'want' to the key_word list – DarkKnight May 06 '22 at 18:56
  • @LancelotduLac adding `"want"` to `key_word` causes `"I want to buy apple vinegar"` to be printed. Is this not the desired result? – JRiggles May 06 '22 at 19:05
  • "I want to buy apple vinegar" will be printed twice – DarkKnight May 07 '22 at 06:50
0

It looks as though you want to print any title that contains any of the key words.

Start by iterating over the titles.

Then:

key_word = ["apple","Apple","Boy","boy"]

title1 = "Where the boy"
title2 = "The Boy playing cricket"
title3 =  "hello world"
title4 = "I want to buy apple vinegar"

for title in title1, title2, title3, title4:
    if any(kw in title for kw in key_word):
        print(title)

Output:

Where the boy
The Boy playing cricket
I want to buy apple vinegar
DarkKnight
  • 19,739
  • 3
  • 6
  • 22