Python Practice › Function Programs › Program 35
Search for words in a string
Write a Python function that reports which of the given words appear in a string.
Input
"Python makes testing easy", ["testing", "java", "easy"]
Output
{'testing': True, 'java': False, 'easy': True}Approach
- Compare lower-case words.
Solution
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}Learn the concept
New to this topic? Read the lesson, then come back.