Kamilche, have you tried module sets? tmp_validchars = string.ascii_letters + string.digits + \ "!@#$%^&*()`~-_=+[{]}\\|;:\'\",<.>/?\t " _validchars=sets.Set(tmp_validchars) ... the test becomes: testset=sets.Set(s) if testset.difference(_validchars): raise Exception("Invalid character found!") it may be even faster... best wishes Harald