zoukankan      html  css  js  c++  java
  • python_内置字符串

    写程序时可能需要所有整数,所有字母等,无需自己新建list或字符串,python中自带这些字符串

    import  string
    
    print(string.digits) #所有整数
    print(string.ascii_letters) #所有小写及大写
    print(string.ascii_lowercase) #所有小写
    print(string.ascii_uppercase) #所有大写
    print(string.punctuation) #所有特殊字符
    
    
    D:studypython	estvenvScriptspython.exe D:/study/python/test/dd.py
    0123456789
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

    例子:

    import  string
    import  random
    
    print(random.sample(string.ascii_uppercase,4)) #大写字母中随机产生4个
    print(random.sample(string.punctuation,8)) #所有特殊字符中随机产生8个
    
    
    D:studypython	estvenvScriptspython.exe D:/study/python/test/dd.py
    ['J', 'M', 'C', 'P']
    ['{', "'", '%', '&', '|', '^', '+', '#']
  • 相关阅读:
    使用扩展运算符实现数组,对象的深拷贝
    将图片文件转成BASE64格式
    怎么让input默认为0
    两次取反的意义
    css世界一
    Q&H
    .gitignore 中添加.vscode 无效
    百度小程序
    定位元素和opacity
    js 设置元素样式
  • 原文地址:https://www.cnblogs.com/xiaokuangnvhai/p/11001934.html
Copyright © 2011-2022 走看看