zoukankan      html  css  js  c++  java
  • python(string 模块)

    1.string 模块下关键字源码定义

    whitespace = ' 	
    
    vf'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_letters = ascii_lowercase + ascii_uppercase
    digits = '0123456789'
    hexdigits = digits + 'abcdef' + 'ABCDEF'
    octdigits = '01234567'
    punctuation = r"""!"#$%&'()*+,-./:;<=>?@[]^_`{|}~"""
    printable = digits + ascii_letters + punctuation + whitespace

    2.ascii_letters

    • 生成所有大小写字母(a-z 和 A-Z)
    import string
    import random
    
    print(string.ascii_letters)
    #随机生成 1 位大写字母或小写字母的字符串
    print(random.choice(string.ascii_letters))
    #随机生成 6 位由大写字母和小写字母组成的字符串
    print("".join(random.choice(string.ascii_letters) for i in range(6)))
    
    #结果如下
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    y
    sgbisi

    3.ascii_lowercase

    • 生成所有小写字母(a-z)
    import string
    import random
    
    print(string.ascii_lowercase)
    #随机生成 1 位小写字母的字符串
    print(random.choice(string.ascii_lowercase))
    #随机生成 6 位由小写字母组成的字符串
    print("".join(random.choice(string.ascii_lowercase) for i in range(6)))
    
    #结果如下
    abcdefghijklmnopqrstuvwxyz
    n
    fhwwnp

    4.ascii_uppercase

    • 生成所有大写字母(A-Z)
    import string
    import random
    
    print(string.ascii_uppercase)
    #随机生成 1 位大写字母的字符串
    print(random.choice(string.ascii_uppercase))
    #随机生成 6 位由大写字母组成的字符串
    print("".join(random.choice(string.ascii_uppercase) for i in range(6)))
    
    #结果如下
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Y
    GEZIJU

    5.digits

    • 生成所有数字(0-9)
    import string
    import random
    
    print(string.digits)
    #随机生成 1 位数字
    print(random.choice(string.digits))
    #随机生成 11 位数字的手机号
    print(random.choice(["133","177","138"]) + "".join(random.choice(string.digits) for i in range(8)))
    #随机生成 6 位由数字/大小写字母组成的字符串
    print(''.join(random.choice(string.ascii_letters + string.digits) for i in range(6)))
    
    #结果如下
    0123456789
    8
    17786145200
    Y4xD4L

    6.punctuation

    • 生成所有标点符号
    import string
    import random
    
    print(string.punctuation)
    #随机生成 6 位标点符号组成的字符串
    print(''.join(random.choice(string.punctuation) for i in range(6)))
    
    #结果如下
    !"#$%&'()*+,-./:;<=>?@[]^_`{|}~
    <!!&[
  • 相关阅读:
    前端资源分享
    Java的wait(), notify()和notifyAll()使用心得(转)
    Java 理论与实践: 处理 InterruptedException(转)
    关于线程中断的总结
    Python入门(good)
    看着自己有什么样的资源,利用好这些资源就好了。不要看着别人的资源流口水(转)
    android手机SD卡中的android_secure目录
    Android中ExpandableListView控件基本使用
    华为的面试经历
    Flex强制类型转换错误
  • 原文地址:https://www.cnblogs.com/ZhengYing0813/p/12530113.html
Copyright © 2011-2022 走看看