zoukankan      html  css  js  c++  java
  • python3----输出所有大小写字母及数字

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

    import string   # 导入string这个模块
    print(string.digits)  # 输出包含数字0~9的字符串
    print(string.ascii_letters)  # 包含所有字母(大写或小写)的字符串
    print(string.ascii_lowercase)  # 包含所有小写字母的字符串
    print(string.ascii_uppercase)  # 包含所有大写字母的字符串
    
    
    ##############
    0123456789
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    print([chr(i) for i in range(65, 91)])  # 所有大写字母
    print([chr(i) for i in range(97, 123)])  # 所有小写字母
    print([chr(i) for i in range(48, 58)])   # 所有数字
    
    ####################
    ['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']
    ['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']
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    2. 列表生成式

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

    3. 生成随机验证码

    import random
    def get_code():
        source = list('0123456789')
        for i in range(97, 123):
            source.append(chr(i))
        print(''.join(random.sample(source, 4)))
    def v_code():
        code = ''
        for i in range(5):
            add = random.choice([random.randrange(10), chr(random.randrange(97, 123))])
            code += str(add)
        print(code)
  • 相关阅读:
    Ant: Class not found: javac1.8
    (转)ant 使用指南
    炼数成金(dataguru)IT技能修炼
    [原创]记我的6年测试经历
    CentOS6.5 安装并配置vsftpd
    炫龙炎魔T1笔记本 Win7 系统安装
    Oracl数据库+PL/SQL安装与配置
    C# 面试题
    Linux安装Python3后,如何使用pip命令
    CentOS7 Python2 和Python3 共存(Python3安装)【转】
  • 原文地址:https://www.cnblogs.com/pyse/p/9847812.html
Copyright © 2011-2022 走看看