Check if a string contains only given characters

前端 未结 5 1494
北恋
北恋 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:29

    Using regular expressions:

    import re
    if re.search('[^abc]', string):
        print('wrong character')
    

提交回复
热议问题