添加生成随机字符串的 Python 脚本

This commit is contained in:
YuCheng Hu 2021-03-18 08:10:12 -04:00
parent f384f7e700
commit 6e88d38f51
No known key found for this signature in database
GPG Key ID: 1E5CBEF8B550FB7D
1 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Python Random String Password
# Author - https://www.ossez.com
import string
from random import choice
def random_password(length, printable):
"""
根据给定的字符串长度和可以用于生成字符串的字符集来生成随机字符串
:param printable: 可用于生成随机字符串的字符
:param int length: 生成随机字符串的数量
"""
return "".join([choice(printable) for x in range(int(length))])
if __name__ == "__main__":
3
amount = int(input("请输入需要生成随机字符串的数量: "))
number = int(input("请输入随机字符串的长度: "))
for i in range(1, amount + 1):
print(f" 随机字符串 [Printable String]: {i} - {repr(random_password(number, string.printable))} ")
print('')
for i in range(1, amount + 1):
print(f" 随机字符串 [Ascii Uppercase String]: {i} - {repr(random_password(number, string.ascii_uppercase))} ")