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>You can use <a href="https://docs.python.org/3.4/howto/regex.html#regex-howto" rel="nofollow">regular expressions</a> to check if every word in your text matches a certain pattern. The pattern in your case is that all characters in a word should be letters of the alphabet : uppercase <strong>A-Z</strong> as well as lowercase <strong>a-z</strong> (i assume from your example) and the letters <strong>ÃµÃ¤Ã¶Ã¼</strong> ).</p>

<p>Learning how to use regular expressions might seem daunting for beginners but with a bit of practice you will find them very useful and efficient.</p>

<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import re

def check_allowed_chars(text):
    """
    """
    pattern = re.compile('[a-zA-ZÃµÃ¤Ã¶Ã¼]+$')
    words = text.split()
    for word in words:
        if not pattern.match(word):
            print('The text contains not allowed characters!')
            return
</code></pre>

python acronym