Checking if input's characters match the ones given in Python
<p>I'm in a Python course for beginners.
We have to create a code that turns input of maximum 6 words into an acronym. </p>

<p>Before creating an acronym, it has to check if the words contain only characters from given set, but I can't just check if it's in alphabet as we are using our local alphabet that has special characters (Ãµ, Ã¤, Ã¶, Ã¼).</p>

<pre><code>def main():

    nr_of_words_limit = 6
    chars = "abcdefghijklmnopqrstuvwÃµÃ¤Ã¶Ã¼xyz"

    def not_allowed_characters_check(text, chars):
        """This checks if all words in text only include characters from chars"""

    def acronym(text, chars, nr_of_words_limit): 
        """Creates acronym after checking for not allowed characters"""
</code></pre>

<p>So, in this case:</p>

<pre><code>text = "Hello World!"
</code></pre>

<p>It would just say that text contains not allowed characters due to the exclamation mark.</p>

<p>How would I go about comparing if every letter in every word in text matches chars then?</p>

<p>Thanks for any help, really appreciate it.</p>

<p>Simplest way is to check if the set of characters in the word is a subset of the alphabet using <code>set(word).issubset(alphabet)</code>. For example:</p>

<pre><code>alpha_set = set("best") 
print set("test").issubset(alpha_set)
print set("testa").issubset(alpha_set)
</code></pre>

<p>prints:</p>

<pre><code>True
False
</code></pre>

<p><a href="https://ideone.com/kEz0IV" rel="nofollow">Example here</a></p>

python acronym