python_内置字符串
写程序时可能需要所有整数,所有字母等,无需自己新建list或字符串,python中自带这些字符串
import string print(string.digits) #所有整数 print(string.ascii_letters) #所有小写及大写 print(string.ascii_lowercase) #所有小写 print(string.ascii_uppercase) #所有大写 print(string.punctuation) #所有特殊字符 D:\study\python\test\venv\Scripts\python.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:\study\python\test\venv\Scripts\python.exe D:/study/python/test/dd.py ['J', 'M', 'C', 'P'] ['{', "'", '%', '&', '|', '^', '+', '#']