import random import string ran_str =
''.join(random.sample(string.ascii_letters + string.digits, 8)) print ran_str

#-*-coding:utf-8 -*- import random import string import sys #存储大小写字母和数字，特殊字符列表
STR = [chr(i) for i in range(65,91)] #65-91对应字符A-Z str = [chr(i) for i in
range(97,123)] #a-z number = [chr(i) for i in range(48,58)] #0-9 #特殊字符串列表获取有点不同
initspecial = string.punctuation #这个函数获取到全部特殊字符，结果为字符串形式 special = [] #定义一个空列表
#制作特殊符号列表 for i in initspecial: special.append(i) total = STR + str + number +
special #print total choices = ['6','8','10','16'] def
random.sample(total,int(your_choice))
''.join(passwordli) #现在得到的是转换后的字符串 ‘’是分隔符，里面可以为; : . 等等 print
"\033[32m生成的\033[0m" + your_choice + "\033[32m位数密码为:\033[0m\n" + passwordst
else: print "\033[31m请输入指定位数(6,8,10,16) \033[0m" if __name__ == '__main__':
while True: choice =
raw_input("\033[33m请输入你要得到随机密码的位数：(6,8,10,16)，或输入q退出\033[0m\n") if choice !=
'q': #输入q则退出循环 Randompassword(choice) #执行函数 else: break

#!/usr/bin/env python # -*- coding=utf-8 -*- import random, string
#导入random和string模块 def GenPassword(length): #随机出数字的个数 numOfNum =
random.randint(1,length-1) numOfLetter = length - numOfNum #选中numOfNum个数字
slcNum = [random.choice(string.digits) for i in range(numOfNum)]
#选中numOfLetter个字母 slcLetter = [random.choice(string.ascii_letters) for i in
range(numOfLetter)] #打乱组合 slcChar = slcNum + slcLetter random.shuffle(slcChar)
#生成随机密码 getPwd = ''.join([i for i in slcChar]) return getPwd if __name__ ==

Toolsou
API参考文档