3

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.

1
  • You can use a regex with negative group: re.fullmatch(r'[^%s]*' % seq_letters, string1) Commented Jul 16, 2019 at 8:10

2 Answers 2

6

Using set.difference

string1 = 'SEQ'
seq_letters = 'ATCGRYMKSWHBVDN'

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

Output:

{'E', 'Q'}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.