DEV Community

Zsolt Szakal
Zsolt Szakal

Posted on

 

The Most Simple Python Random Password Generator - Only 6 lines of code

The most simple way to generate a random password in Python.

  1. import string
  2. import random
  3. import secrets
  4. with string create possible charachters
  5. create password
  6. print password to the console
import string
import random
import secrets

password_chars = string.ascii_letters + string.digits + string.punctuation

password = "".join([secrets.choice(password_chars) for i in range(random.randint(12, 15))])

print(password)
Enter fullscreen mode Exit fullscreen mode

or to shorten it even more:

import string
import random
import secrets
password = "".join([secrets.choice(string.ascii_letters + string.digits + string.punctuation) for i in range(random.randint(12, 15))])
print(password)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.