Simplest Python Random Password - with only 6 lines of code

zsoltszakal

Zsolt Szakal

Posted on March 13, 2021

Simplest Python Random Password - with only 6 lines of code

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

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

password_chars = string.ascii_letters + string.digits + string.punctuation
length = random.randint(12, 15)

password = "".join([random.choice(password_chars) for _ in range(length)])

print(password)
Enter fullscreen mode Exit fullscreen mode

Or a single line password generator below:

import string
import random

print("".join([random.choice(string.ascii_letters + string.digits + string.punctuation) for _ in range(random.randint(12, 15))]))
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
zsoltszakal
Zsolt Szakal

Posted on March 13, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related