[python]随机生成16位密码,至少包含大小写字母,数字,符号,并打印不同字符出现的概率

import random
import string
def generate_password(length=16):
    characters = string.ascii_letters + string.digits + string.punctuation
    password = random.choice(string.ascii_uppercase)
    password += random.choice(string.ascii_lowercase)
    password += random.choice(string.digits)
    password += random.choice(string.punctuation)
    for i in range(length - 4):
        password += random.choice(characters)
    password_list = list(password)
    random.shuffle(password_list)
    return ''.join(password_list)
password = generate_password()
print("随机密码为:", password)

# 统计不同类型字符的个数
num_uppercase = sum(1 for c in password if c.isupper())
num_lowercase = sum(1 for c in password if c.islower())
num_digits = sum(1 for c in password if c.isdigit())
num_symbols = sum(1 for c in password if c in string.punctuation)
total = num_uppercase + num_lowercase + num_digits + num_symbols
# 计算比例
ratio_uppercase = num_uppercase / total
ratio_lowercase = num_lowercase / total
ratio_digits = num_digits / total
ratio_symbols = num_symbols / total
print("大写字母比例:", ratio_uppercase)
print("小写字母比例:", ratio_lowercase)
print("数字比例:", ratio_digits)
print("符号比例:", ratio_symbols)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值