简体   繁体   English

Python 上的随机密码生成器

[英]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 "*!@#$%^&"正如 quamrana 所述,您还可以使用string.punctuation代替"*!@#$%^&"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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