Create a random character password generator. With 12 in size, with the following rules:
12 characters: 3 uppercase letters, 2 symbols, 3 numbers and the rest lowercase letters.
For example: Bj&Pr5Ru$2io
I'm trying, but I'm not able to make this specific rule. Someone could help with that ending. I got here as follows:
import string
from random import choice
print ('Welcome Password Generator')
print('='*30)
string.ascii_lowercase
string.ascii_uppercase
string.ascii_letters
string.digits
string.punctuation
size = 12
v = string.ascii_uppercase string.punctuation string.digits string.ascii_lowercase
password = ''
for i in range(size):
password = choice(v)
print(f'Generated Password: {password}' )
CodePudding user response:
First build a list of characters using the appropriate number of each character category, then shuffle the list and join the characters into a single string for the password:
import string
from random import choices,sample
chars = choices(string.ascii_uppercase,k=3) # 3 uppercase letters
chars = choices(string.punctuation,k=2) # 2 symbols
chars = choices(string.digits,k=3) # 3 numbers
chars = choices(string.ascii_lowercase,k=4) # rest lowercase
password = "".join(sample(chars,12)) # shuffle into a string
print(f'Generated Password: {password}' )
Generated Password: 7Qc$S1v5-Gqu