Check if a string contains only given characters

前端 未结 5 1496
北恋
北恋 2020-12-15 13:51

I\'m wondering if there is more elegant way to check if the string (str = \'abcccbbaabcbca\') contains only \'a\',\'b\' or \'c\' than iterating over it :

for         


        
5条回答
  •  我在风中等你
    2020-12-15 14:46

    You could use any with a generator expression:

    if any(c not in 'abc' for c in _str):  # Don't use str as a name.
        print('Wrong character')
    

提交回复
热议问题