Homap
Homap

Reputation: 2214

check if a string contains character except the ones in a given string python

How can I check if a string (string1) contains characters except the ones in the following string (seq_letters):

string1 = 'SEQ'

seq_letters = 'ATCGRYMKSWHBVDN'

E and Q are not in seq_letters.

Upvotes: 3

Views: 976

Answers (2)

shubham
shubham

Reputation: 513

string1 = 'SEQ'
seq_letters = 'ATCGRYMKSWHBVDN'
result = []
for i in string1:
    if i not in seq_letters:
        result.append(i)
print(result)

Upvotes: 1

Rakesh
Rakesh

Reputation: 82765

Using set.difference

string1 = 'SEQ'
seq_letters = 'ATCGRYMKSWHBVDN'

print(set(string1).difference(seq_letters))

Output:

{'E', 'Q'}

Upvotes: 6

Related Questions