0
点赞
收藏
分享

微信扫一扫

python生成随机字符串(密码)

ivy吖 2022-04-14 阅读 127
python

背景:通过python脚本生成生成字符串密码。

系统环境:windows11

python版本: 3.8

import random
import string


def generate_random_password(count, length):
	# string.ascii_letters 大小写字母, string.digits 为数字
	characters_long = list(string.ascii_letters + string.digits + "!@#$%^&*()")

	# 打乱字符串序列
	random.shuffle(characters_long)
	
	# 生成密码个数
	for i in range(int(count)):
		# picking random characters from the list
		password = []
		# 生成密码个数
		for b in range(length):
			password.append(random.choice(characters_long))

		# 打乱密码顺序
		random.shuffle(password)

		# 将列表转换为字符串并打印
		print("".join(password))


def main():
	length = int(input("请输入密码长度: "))
	count = int(input("请输入密码生成个数: "))
	generate_random_password(count, length)


if __name__ == '__main__':
	main()

参考文档
https://geekflare.com/password-generator-python-code/

举报

相关推荐

0 条评论