Return True if all characters in a string are in another string

后端 未结 2 921
攒了一身酷
攒了一身酷 2020-12-21 00:16

Alright so for this problem I am meant to be writing a function that returns True if a given string contains only characters from another given string. So if I input \"bird\

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 00:30

    This is a perfect use case of sets. The following code will solve your problem:

    def only_uses_letters_from(string1, string2):
       """Check if the first string only contains characters also in the second string."""
       return set(string1) <= set(string2)
    

提交回复
热议问题