简体   繁体   中英

Random Password Generator on Python

I am trying to create a random password generator. My question is "How can I add random digits and symbols to the password?" Right now this is my code, as you can see it only gives me random letters.

a = random.SystemRandom() 

length = 10

alphabet = string.ascii_letters + string.digits

password = str().join(a.choice(alphabet) for _ in range(length))

print(password)

If you want to concat to the end

symbols = "*!@#$%^&"
for i in range(1): #change the number to the desired amount of symbols to add to the end
    password += a.choice(symbols)

or if you want them randomly placed:

symbols = "*!@#$%^&"
alphabet = string.ascii_letters + string.digits + symbols*2 #adding them in twice to reduce the probability of getting a password without them

As stated by quamrana you can also use string.punctuation in place of "*!@#$%^&"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM