今天想弄个生成随机密码的小工具,发现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)
打印结果如下