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

asked Mar 19, 2014 at 15:37
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
answered Mar 19, 2014 at 15:41
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
>>>
answered Mar 19, 2014 at 15:38

Comments

2

Try this

for i in s:
 if i in d:
 print i
IVlad
43.6k13 gold badges115 silver badges185 bronze badges
answered Aug 18, 2015 at 9:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.