案例:生成随机密码
#!/usr/local/bin/python3
#-*- coding: utf-8 -*-
from random import choice
from string import ascii_letters , digits
def mk_rpass(n=8):
# 定义字符集
zifuji = ascii_letters + digits
# 用变量接收结果
result = ''
# 循环取数
for i in range(n) :
ran = choice(zifuji)
result += ran
return result
# 测试
if __name__ == '__main__':
a = mk_rpass()
b = mk_rpass(4)
print(a,b)