hardCodedSetOfAllowedCharacters = set(c for c in
    '0123456789a...zA...Z~!@#$%^&*()_+')

def getPassword():
    password = input('Enter string to test: ').strip()
if len(password) < 8:
    raise AppropriateError('password is too short')
if any(passChar not in hardCodedSetOfAllowedCharacters for passChar in password
    ):
    raise AppropriateError('password contains illegal characters')
return password
Как проверить пароль регулярного выражения в Python?