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))]))
Top comments (2)
This is what I love about stumbling upon random stuff like this. I've been a Python developer for two decades, and yet had no idea that
random.choices
(the plural) existed. Looks like it came in 3.6. Thanks!