python生成随机密码
作者:
Aedda | 来源:发表于
2022-01-14 16:01 被阅读0次import random
import string
def create_code(digit):
'''
string.printable 生成一个包含全部字符的字符串
string.whitespace 生成一个包含空格、换行等空字符的字符串
原理:全部字符 去掉 空的字符串 就是我们需要的,把字符串转为列表方便一会儿使用
'''
example = list(string.printable.replace(string.whitespace, ''))
'''
random.choice(example) 从列表中随机获取的一个元素
''.join(列表) 把列表中的元素 以''空字符串连接 输出一个新的字符串
[random.choice(example) for i in range(digit)] 列表推导式
'''
code = ''.join([random.choice(example) for i in range(digit)])
return code
本文标题:python生成随机密码
本文链接:https://www.haomeiwen.com/subject/ynwacrtx.html
网友评论