Simplest Python Random Password - with only 6 lines of code
Zsolt Szakal
Posted on March 13, 2021
The most simple way to generate a random password in Python.
- import string
- import random
- with string create possible charachters
- with random generate a random number
- create password
- 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)
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))]))
💖 💪 🙅 🚩
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.