string模块中定义了一些常用的属性,包含所有数字、字母、可打印的所有ascii码等
实例
- ascii_letters 生成所有大小写字母(a-z A-Z)
1
2
3
4
5
6import string
letters = string.ascii_letters
print(letters)
>>> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
ascii_lowercase 生成所有小写字母(a-z)
1
2
3
4
5
6import string
lowercase = string.asscii_lowercase
print(lowercase)
>>> abcdefghijklmnopqrstuvwxyzascii_uppercase 生成所有大写字母(A-Z)
1
2
3
4
5
6import string
uppercase = string.ascii_uppercase
print(uppercase)
>>> ABCDEFGHIJKLMNOPQRSTUVWXYZdigits 生成所有数字(0-9)
1
2
3
4
5
6import string
digits = string.digits
print(digits)
>>> 0123456789punctuation 生成所有标点符号
1
2
3
4
5
6import string
punctuation = string.punctuation
print(punctuotion)
>>> !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
#密码生成器
1
2
3
4
5
6def main():
"""密码生成器"""
a = string.ascii_letters + string.digits + string.punctuation
key = random.sample(a, 16)
keys = "".join(key)
print(keys)