Search for words in a string

EasyFunction Programs · 35 of 39

Write a Python function that reports which of the given words appear in a string.

Advertisement
Input
"Python makes testing easy", ["testing", "java", "easy"]
Output
{'testing': True, 'java': False, 'easy': True}

Approach

  1. Compare lower-case words.

Solution

Python
def search(text, words):    found = set(text.lower().split())    return {w: w.lower() in found for w in words} print(search("Python makes testing easy", ["testing", "java", "easy"]))
Output
{'testing': True, 'java': False, 'easy': True}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs