Muốn check ký tự cho trước có tồn tại trong string, dùng in. Ví dụ:
>>> s = "Hello world" >>> 'e' in s True
Nếu có list ký tự muốn tìm, dùng Sets. Thêm ký tự vào set và dùng function any check xem ký tự nào tồn tại trong string. Ví dụ:
from sets import Set
chars = Set('0123456789$,')
s = "I have 9 cats"
if any((c in chars) for c in s):
print('Found')
else:
print('Not Found')
Output:
Found
Muốn check tất cả ký tự đó tồn tại trong string, thay any bằng all. Ví dụ:
from sets import Set
chars = Set('0123456789$,')
s = "I have 9 cats"
if all((c in chars) for c in s):
print('Found')
else:
print('Not Found')
Output:
Not Found