Python生成随机验证码

生成随机验证码代码

方法一:

1
2
3
4
5
6
7
8
9
10
11
# 生成随机码
import random #random标准库
#创建一个随机生成元素列表
all_raw_code=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
my_code=[]  #列表
for i in range(4):
    #注意缩进语法 indent (从属关系)(父子关系)
    gen_code=all_raw_code[random.randint(0,len(all_raw_code)-1)] #列表下标取值知识点
    print(gen_code)
    my_code.append(gen_code) #列表添加知识点
print('我的提取码为:',''.join(my_code));

方法二:

1
2
3
4
5
6
7
import random
import string
s=''.join(random.sample((string.ascii_lowercase+string.digits),4))
print(s)
#数字切片,只取0-4的值
t=''.join(random.sample((string.ascii_lowercase+string.digits[0:4]),4))
print(t) 
posted @   leagueandlegends  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
· 易语言 —— 开山篇
点击右上角即可分享
微信分享提示