DEV Community

Angela
Angela

Posted on

2 1

Generate random passwords in Python using secrets

Read the docs: https://docs.python.org/3/library/secrets.html

I copied this example from python docs and added special characters to the password.

import secrets
import string


alphabet = string.ascii_letters + string.digits + '!@#$%^&*()-+[]'
while True:
    password = ''.join(secrets.choice(alphabet) for i in range(10))
    if (any(c.islower() for c in password)
            and any(c.isupper() for c in password)
            and any(c.isalnum() for c in password)
            and any(not(c.isalnum()) for c in password)
            and any(c.isdigit() for c in password)):
        break
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post