title: generating random string with requirements in Python 3
body: <p>I'm trying to generate random passwords for the Active Directory that has the following password requirements: at least 8 characters, at least one special character, at least one digit, at least one lowercase and at least one uppercase.</p>
  
  <p>With the following code I'm able to generate a random password and check whether it contains a special character. New password is generated until a special character is found.</p>
  
  <pre><code>special_char = "!@%/()=?+.-"
  
  password_string = "".join([random.choice(string.ascii_lowercase + string.ascii_digits + string.ascii_uppercase + special_char) for n in range(8)])
  
  while any(c in password_string for c in special_char) is not True:
      password_string = "".join([random.choice(string.ascii_lowercase + string.ascii_digits + string.ascii_uppercase + special_char) for n in range(8)])
  </code></pre>
  
  <p>The problem with the following is that it's only checking for the special character and generating a new password might get rid of the other requirements assuming they existed. How could I implement the AD password requirements efficiently? Thanks for any help.</p>