Welcome, guest | Sign In | My Account | Store | Cart

Generates a really strong and secure password. It uses multiple characters and symbols. Remember to score please! It really means a lot to me. Revision 4 includes an input so you can choose how many characters you want your password to have. It is also faster due to a suggestion by Garel Alex. Thanks!

Python, 26 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
##Author: ATC
##Please score this on activestate

import string, random

print "How many characters would you like the password to have?"
print "Must be nine or more"
length = input ()


password_len = length

password = []

for group in (string.ascii_letters, string.punctuation, string.digits):
    password += random.sample(group, 3)

password += random.sample(
                 string.ascii_letters + string.punctuation + string.digits,
                 password_len - len(password))

random.shuffle(password)
password = ''.join(password)


print password

Any suggestions?

4 comments

Garel Alex 12 years, 5 months ago  # | flag

Better than checking afterwards, meet the requirement from the begining :

import string, random

password_len = 30

password = []

for group in string.ascii_letters + string.punctuation + string.digits:
    password += random.sample(group, 3)

password += random.sample(
                 string.ascii_letters + string.punctuation + string.digits,
                 password_len - len(password))

random.shuffle(password)
password = ''.join(password)
Garel Alex 12 years, 5 months ago  # | flag

sorry instead of:

for group in string.ascii_letters + string.punctuation + string.digits:

It is:

for group in (string.ascii_letters, string.punctuation, string.digits):
Alexander James Wallar 12 years, 4 months ago  # | flag
from random import *
passwordGen = lambda length, badChars = '',alpha = '1234567890qwertyuiop[]asdfghjkl;zxcvbnm,.!@#$%^&*()_+-=-{}:<>|QWERTYUIOPASDFGHJKLZXCVBNM~`?': "".join([list(set(alpha)^set(badChars))[randint(0,len(list(set(alpha)^set(badChars)))-1)] for i in range(length)])
Captain DeadBones 11 years, 2 months ago  # | flag

Thank you for your code. Try looking at Generating And Checking Passwords In Python

Created by Alexander Thomas Cruz on Wed, 12 Oct 2011 (MIT)
Python recipes (4591)
Alexander Thomas Cruz's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks