Python Practice › Set Programs › Program 40

Set elements found in a string

EasySet Programs · 40 of 48

Write a Python program to find which elements of a set appear in a string.

Advertisement
Input
{"py", "java", "test"}, "python testing"
Output
['py', 'test']

Approach

  1. [w for w in s if w in text].

Solution

Python
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.

More set programs

← All 48 Set Programs

Leave a Comment