Reputation: 229
let two strings
s='chayote'
d='aceihkjouty'
the characters in string s
is present in d
Is there any built-in python function to accomplish this ?
Thanks In advance
Upvotes: 0
Views: 83
Reputation: 262919
Using sets:
>>> set("chayote").issubset("aceihkjouty")
True
Or, equivalently:
>>> set("chayote") <= set("aceihkjouty")
True
Upvotes: 5
Reputation:
I believe you are looking for all
and a generator expression:
>>> s='chayote'
>>> d='aceihkjouty'
>>> all(x in d for x in s)
True
>>>
The code will return True
if all characters in string s
can be found in string d
.
Also, if string s
contains duplicate characters, it would be more efficient to make it a set using set
:
>>> s='chayote'
>>> d='aceihkjouty'
>>> all(x in d for x in set(s))
True
>>>
Upvotes: 4