ETechSavvies

Generate a random password

Apr 28th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. """
  2.    Written by: ETechSavvies [https://t.me/ETechSavvies] Apr 10,2021
  3.    Description: Random Password Generator
  4.    Copyright 2021 @ETechSavvies
  5. """
  6.  
  7. import random, string
  8.  
  9. def password_generator(length):
  10.  
  11.     if length < 4:
  12.         return "Password length can't be lessthan 4"
  13.  
  14.     # This our dataset composed of letters, digits and symbols
  15.     lower = string.ascii_lowercase
  16.     upper = string.ascii_uppercase
  17.     digits = string.digits
  18.     symbols = string.punctuation
  19.  
  20.     combined_data = lower + upper + digits + symbols
  21.  
  22.     # This is to ensure that we have at least one character from each set of characters
  23.     rand_lower = random.choice(lower)
  24.     rand_upper = random.choice(upper)
  25.     rand_digits = random.choice(digits)
  26.     rand_symbols = random.choice(symbols)
  27.  
  28.     # We have 4 characters in the password now
  29.     temp_pwd1 = list(rand_lower + rand_upper + rand_digits + rand_symbols)
  30.     random.shuffle(temp_pwd1)
  31.  
  32.     # Fill in the rest of the characters by selecting randomly from the combined_data
  33.     temp_pwd2 = random.sample(combined_data, length - 4)
  34.  
  35.     return "".join(temp_pwd1 + temp_pwd2)
  36.  
  37.  
  38.  
  39. length = int(input("Enter the length of the password you want to generate: "))
  40.  
  41. print(password_generator(length))
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment