1.string 模块下关键字源码定义
whitespace = ‘ \t\n\r\v\f‘ 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
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
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
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
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))) #结果如下 !"#$%&‘()*+,-./:;<=>?@[\]^_`{|}~ <!!\&[
原文:https://www.cnblogs.com/ZhengYing0813/p/12530113.html