Q:

Search for forbidden words in string

I would like to detect a vulgar word in a string. I have a database of indecent words stored in .txt file. This file I put into my .py file.
Example bad word:
words = "word, something, dog, cat ...."

input data string are for example:
input = "xxxxdogxxx" or "dogxxxx" or "xxxdog" or "dog" "ok_word" # xxx == any word

and I want, when input contains the word in word return is False
def check(input):
    word = "something, something, ..."
    x = ???? # I do not know how to do it, probably using regular expressions: re.match?
    if x:
        return False
    else:
        return True

A:

You actually do not need Regex for this.  You can use all and a generator expression:
def check(input):
    return all(x not in input for x in bad_words)

To make your string of words into a list, use str.split:
>>> words = "word, something, dog, cat"
>>> words.split(', ')
['word', 'something', 'dog', 'cat']
>>>

You might also want to call set() on the list to improve lookup times.  Sets have O(1) (constant) complexity with the in and not in operators while lists have O(n) (linear).  So, it is faster to find things in a set than it is with a list.

