Python:3种方式实现随机生成8位字符
方法一:
import random
seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-"
sa = []
for i in range(8):
sa.append(random.choice(seed))
result = ''.join(sa)
print(result)
方法二:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import random
import string
result = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print(result)
方法三:
import random
import string
result = ''.join(random.sample(string.digits, 8))
print(result)