导读 利用Python库random,string生成大小写字母和数字的随机验证码
import random
import string

def generate_code(bit_num):
'''
:param bit_num: 生成验证码位数
:return: 返回生成的验证码
'''

all_str = string.printable.split('!')[0]
code = ''.join([random.choice(all_str) for i in range(bit_num)])

return code

if __name__ == '__main__':
code = generate_code(6)
print(code)

生成带特殊符号的验证码如下

import random
import string

def generate_code(bit_num):
'''
:param bit_num: 生成验证码位数
:return: 返回生成的验证码
'''

all_str = string.printable
code = ''.join([random.choice(all_str) for i in range(bit_num)])

return code

if __name__ == '__main__':
code = generate_code(6)
print(code)

原文来自:https://www.linuxidc.com/Linux/2019-09/160699.htm

本文地址:https://www.linuxprobe.com/python-string-num.html编辑:九十,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/