Randomly generate passwords (Python)

Random password generation is a common encryption technology that helps users generate strong passwords when creating accounts, thereby protecting the security of their accounts.

Randomly generate password. Write a program to randomly generate 10 8-digit passwords from a list of 26 uppercase and lowercase letters and 9 numbers.

method one:

import random
n=8     #每个密码8位
k=10    #10个密码
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l=list(range(0,10))
"""[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
"""
for x in range(65,91):
    l.append(chr(x))
"""
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
"""
for x in range(97,123):
    l.append(chr(x))
"""

"""
for m in range(k):
    key=''
    for i in range(n):
        key=key+str(random.choice(l))    #强制转成字符,str()
    print('第{}密码是:{}'.format(m+1,key))

operation result:

第1密码是:3vPGPR3O
第2密码是:jm90IIIX
第3密码是:VMq0Y7Me
第4密码是:ShodWAOk
第5密码是:VSWEE3ul
第6密码是:HpLInI1A
第7密码是:x0v3BOR9
第8密码是:uTHnGbWc
第9密码是:mfZcA2tD
第10密码是:vw1j9kxr

Method Two:

Specific steps:
1. Import the random module for generating random numbers
2. Define a letter containing 26 uppercase and lowercase letters and 9 List of numbers
3. Use a loop to generate 10 8-digit passwords, one password for each loop
4. In each loop, use the random module The choice function randomly selects 8 characters from the list and splices them into a string, which is an 8-digit password
5. Output the 10 generated passwords

import random

#定义一个包含26个字母大小写和9个数字的列表
characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
#循环生成10个8位密码
for i in range(10):
    #从列表中随机选择8个字符,并将他们拼接成一个字符串
    password = ''.join(random.choice(characters) for i in range(8))
    #输出生成的密码
    print ("随机生成的第{}个8位的密码为:{}".format(i+1,password))

The random.choice function randomly selects a character from the characters list each time, and then outputs these 8 randomly selected characters into a string.

Guess you like

Origin blog.csdn.net/greatau/article/details/134064350