Python Practice › Set Programs › Program 40
Set elements found in a string
Write a Python program to find which elements of a set appear in a string.
Input
{"py", "java", "test"}, "python testing"Output
['py', 'test']
Approach
[w for w in s if w in text].
Solution
words = {"py", "java", "test"}text = "python testing"print(sorted(w for w in words if w in text))Output
['py', 'test']
Learn the concept
New to this topic? Read the lesson, then come back.