python生成随机密码

今天想弄个生成随机密码的小工具,发现python写起来真实简单,基本两行代码就搞定了,其它的程序还得循环,随机函数等等七八行代码。

把代码上下,并记录下来

#!/usr/local/bin/python3
#coding:utf-8
import random

str = 'abcdefghigklmnopqrstuvwxyz1294567890ABCDEFGHIGKLMNOPQRSTUVWXYZ!@#$%&*(),.?'
n = "".join(random.sample(str,20))
print(n)

另一种写法:

#!/usr/local/bin/python3
#coding:utf-8
import random

str = 'abcdefghigklmnopqrstuvwxyz1294567890ABCDEFGHIGKLMNOPQRSTUVWXYZ!@#$%&*(),.?'
length = len(str) - 1
i,n = 0,""
while i < 20:
    n = n + str[random.randint(0, length)]
    i = i + 1
print(n)

打印结果如下

TIM截图20170920143237.jpg

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/428.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。