Hash
Hash

Reputation: 229

Checking two string in python?

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

Answers (3)

fahad
fahad

Reputation: 3179

Try this

for i in s:
    if i in d:
        print i

Upvotes: 2

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

Using sets:

>>> set("chayote").issubset("aceihkjouty")
True

Or, equivalently:

>>> set("chayote") <= set("aceihkjouty")
True

Upvotes: 5

user2555451
user2555451

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

Related Questions