import random,string #调用random、string模块 def create_password(): src_digits = string.digits #string_数字 src_uppercase = string.ascii_uppercase #string_大写字母 src_lowercase = string.ascii_lowercase #string_小写字母 digits_num = random.randint(1,6) uppercase_num = random.randint(1,8-digits_num-1) lowercase_num = 8 - (digits_num + uppercase_num) #生成字符串 password = random.sample(src_digits,digits_num) + random.sample(src_uppercase,uppercase_num) + random.sample(src_lowercase,lowercase_num) #打乱字符串 random.shuffle(password) #列表转字符串 new_password = ''.join(password) print(new_password) return new_password if __name__ == '__main__': create_password()