首先是引进string库和random库(产生随机数的库),然后通过string.ascii_letters与string.ascii_digits引进大小写字母和数字,最后使用random库的sample()返回随机k个元素。
具体代码如下:
import random import string a=string.ascii_letters+string.digits key=[] def getKey(): key=random.sample(a,8) keys="".join(key) return key for i in range(1000): print(getKey())
推荐教程:python教程
以上就是python如何生成随机密码的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号