string contains all chars

Go Back home

There are various approaches to help us ascertain whether all characters in ar appear in the string st. The first is to use a generator expression that checks the existance using the in keyword, and being input into all.

1 st = 'hello'
2 ar = ['e', 'l']
3 print(all(c in st for c in ar))

We can also use str.count with map, which effectively also returns a generator containing the counts of each character of ar in st.

1 st = 'hello'
2 ar = ['e', 'l']
3 print(all(map(st.count, ar)))