Alex
Alex

Reputation: 3

How to check that none of the chars are in the string?

def lookup(content):
    value=True
    if any(x not in content for x in ['A','B','C']):
        value = False
    print (value)

What i want is check if any of these 'A' 'B' 'C' is in a string , for example if string equates ABCAA then the value will be true, if the string is ABDC then value is wrong because content contains a char not defined in my list above.The issue is i'm getting false with that function for 'ABC' which isn't supposed to happen.

Upvotes: 0

Views: 120

Answers (2)

gommb
gommb

Reputation: 1117

Try this:

def lookup(content):
    print(all(x in ['A','B','C'] for x in content))

lookup('ABC') #output: True

Upvotes: 0

Reut Sharabani
Reut Sharabani

Reputation: 31339

Just use sets:

# input
my_string = 'abcd'

# set of allowed characters
approved_characters = set('abc')

# characters in string that are not in set of approved characters
unapproved_characters = set(my_string) - approved_characters # gives {'d'}

Upvotes: 1

Related Questions