In Python, how can I check that a string does not contain any string from a list?

StringsOnFire picture StringsOnFire · Aug 20, 2015 · Viewed 38.2k times · Source

For example, where:

list = [admin, add, swear]
st = 'siteadmin'

st contains string admin from list.

  • How can I perform this check?
  • How can I be informed which string from list was found, and if possible where (from start to finish in order to highlight the offending string)?

This would be useful for a blacklist.

Answer

Hooting picture Hooting · Aug 20, 2015
list = ['admin', 'add', 'swear']
st = 'siteadmin'
if any([x in st for x in list]):print "found"
else: print "not found"

You can use any built-in function to check if any string in the list appeared in the target string