Secure Password Generator using Python

We all use secret codes to secure our accounts and information and protect them from being hacked. Strong passwords are difficult to be hacked. We will go through a program to generate completely random and strong passwords, It’s a Random password Generator using Python.

Conventions for a strong  password

• Password should contain an UpperCase Character.
• Password should contain a LowerCase Character.
• Password should contain a Digit.
• Password should contain a Special Character.
• Password should contain at least 8 Characters.

This Program keeps up to generate a strong password as per your requirements and also helps to save the passwords to a txt file for you convenience. You can save the file in a secure location.

import random
import string

Details=input("Please Enter the Details for Password,such as Site and Username : ")
Length=int(input("Please Enter the password length required: "))

# Get the Characters

lcase=string.ascii_lowercase # All LowerCase Characters
ucase=string.ascii_uppercase # All UpperCase Characters 
digi=string.digits           # All Digits
symbo=string.punctuation     # All Special Characters

# Combining All
all=lcase+ucase+digi+symbo

# Extracting a Random Character from Each
random_lcase=random.choice(lcase)  # Eg: a
random_ucase=random.choice(ucase)  # Eg: Z
random_digi=random.choice(digi)    # Eg: 9
random_symbo=random.choice(symbo)  # Eg: &

# Combining the random charcaters: Eg: aZ9&
# Now, We have a combination of random characters of each type 
chars=random_lcase+random_ucase+random_digi+random_symbo

# Get a string with characters of intended length
for x in range(Length-4):
    chars=chars+random.choice(all)

# Rndomize the string to get the completely random and a strong password
password=''.join(random.sample(chars,len(chars)))
print(password)

f = open("MyFile.txt", "a")# in append mode
f.write(Details+" : "+password+"\n")
f.close()
print("Details saved to MyFile.txt")