生成随机验证码

 1 # import random
 2 
 3 # list1 = []
 4 # # for循环遍历ASCII追加到空列表中
 5 # for i in range(48, 58):  # 数字0-9
 6 #     list1.append(chr(i))
 7 # for i in range(65, 91):  # 对应26个小写英语字母
 8 #     list1.append(chr(i))
 9 # for i in range(97, 123):  # 对应26个大写英语字母
10 #     list1.append(chr(i))
11 # ma = random.sample(list1, 6)
12 # print('获取的结果为:', ma)
13 # ma = ''.join(ma)
14 # print('转化为字符串:', ma)
15 # 第二种方法
16 import random
17 import string
18 
19 str1 = '0123456789'
20 str2 = string.ascii_letters  # 包含所有字母的字符串(包括大小写)
21 str3 = str1 + str2
22 ma1 = random.sample(str3, 6)
23 ma1 = ''.join(ma1)
24 print(ma1)
原文地址:https://www.cnblogs.com/gzj137070928/p/13819999.html