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

后端 未结 2 915
攒了一身酷
攒了一身酷 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:40

    sets are fine, but aren't required (and may be less efficient depending on your string lengths). You could also do simply:

    s1 = "bird"
    s2 = "irbd"
    
    print all(l in s1 for l in s2)  # True
    

    Note that this will stop immediately as soon as a letter in s2 isn't found in s1 and return False.

提交回复
热议问题