Implementation code of python randomly generating 10 digit password

  • 2021-07-03 00:16:45
  • OfStack

Randomly generate 10-digit passwords, letter and number combinations


import string
>>> import random
>>> pwd = ""
>>> letters=string.ascii_letters+string.digits
>>> for i in range(10):
...   letter=random.choice(letters)
...   pwd += letter
...
>>> print(pwd)

Generate by derivation list


 "".join([random.choice(string.ascii_letters+string.digits) for i in range(10)])

PS: Let's take a look at Python generating random passwords

1. What you want to do to generate a random password:

1. Input times, how many pieces of data are generated as many times as you input

2. It is required that the password must contain uppercase letters, lowercase letters and numbers, with a length of 8 digits and cannot be repeated

2. Implementation code


import random,string
src = string.ascii_letters + string.digits
count = input(' Please confirm how many passwords you want to generate:  ')
list_passwds = []
for i in range(int(count)):
  list_passwd_all = random.sample(src, 5) # Randomly take from letters and numbers 5 Bit 
  list_passwd_all.extend(random.sample(string.digits, 1)) # Let the password 1 Fixed inclusion number 
  list_passwd_all.extend(random.sample(string.ascii_lowercase, 1)) # Let the password 1 Definitely contain lowercase letters 
  list_passwd_all.extend(random.sample(string.ascii_uppercase, 1)) # Let the password 1 Contains capital letters 
  random.shuffle(list_passwd_all) # Disrupt the order of the list 
  str_passwd = ''.join(list_passwd_all) # Convert a list to a string 
  if str_passwd not in list_passwds: # Determining whether a duplicate password is generated 
    list_passwds.append(str_passwd)
print(list_passwds)

3. Using the intersection operation of sets to realize


import random,string
passwds = [] # Save passwords that meet the requirements 
count = input(' Please confirm how many passwords you want to generate:  ')
i = 0 # Record the number of passwords that meet the requirements 
while i < int(count):
  passwd = set(random.sample(string.ascii_letters + string.digits,8)) # Random extraction from letters and numbers 8 Bit generated cipher 
  if passwd.intersection(string.ascii_uppercase) and passwd.intersection(string.ascii_lowercase) and passwd.intersection(string.digits): # Determine whether the password contains upper and lower case letters and numbers 
    passwds.append(''.join(passwd)) # Convert a collection to a string 
    i += 1 # Every generation 1 A password that meets the requirements, i Plus 1
print(passwds)

4. Using regular expressions to implement


import re, random, string
count1 = int(input(' Please enter the number of passwords ( Must be greater than 0) :  '))
i = 0
passwds = []
while i < count1:
  tmp = random.sample(string.ascii_letters + string.digits, 8)
  passwd = ''.join(tmp)
  if re.search('[0-9]', passwd) and re.search('[A-Z]', passwd) and re.search('[a-z]', passwd):
    passwds.append(passwd)
    i += 1
print(passwds)

Summarize


Related articles: