python3----输出所有大小写字母及数字

1. 用一行输出所有大(小)写字母,以及数字

1 print([chr(i) for i in range(65, 91)])  # 所有大写字母
2 print([chr(i) for i in range(97, 123)])  # 所有小写字母
3 print([chr(i) for i in range(48, 58)])   # 所有数字
4 
5 ####################
6 ['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']
7 ['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']
8 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

 

 1 import string   # 导入string这个模块
 2 print(string.digits)  # 输出包含数字0~9的字符串
 3 print(string.ascii_letters)  # 包含所有字母(大写或小写)的字符串
 4 print(string.ascii_lowercase)  # 包含所有小写字母的字符串
 5 print(string.ascii_uppercase)  # 包含所有大写字母的字符串
 6 
 7 
 8 ##############
 9 0123456789
10 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
11 abcdefghijklmnopqrstuvwxyz
12 ABCDEFGHIJKLMNOPQRSTUVWXYZ

2. 列表生成式

1 a = [str(x) for x in range(10)]
2 
3 ######################
4 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

 

 

3. 生成随机验证码

1 import random
2 def get_code():
3     source = list('0123456789')
4     for i in range(97, 123):
5         source.append(chr(i))
6     print(''.join(random.sample(source, 4)))

 

1 def v_code():
2     code = ''
3     for i in range(5):
4         add = random.choice([random.randrange(10), chr(random.randrange(97, 123))])
5         code += str(add)
6     print(code)

 

转载于:https://www.cnblogs.com/jonm/p/8448118.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值