0

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

1
  • 1
    To help people help you, it's usually a good idea to be even more specific. By "the characters in string s present in d", do you mean you care, or don't care, about multiplicity? For example, if s = "aabbcc" and d = "abc", do you want True (because a, b, and c are in d), or False, because there are 2 a characters in s and only 1 in d? Commented Mar 19, 2014 at 15:45

3 Answers 3

5

Using sets:

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

Or, equivalently:

>>> set("chayote") <= set("aceihkjouty")
True
Sign up to request clarification or add additional context in comments.

1 Comment

You beat me...this is the classical way to do it because the problem is inherently a problem of Set theory.
4

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

Comments

2

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.