Random Password Generator with Python
The Below code snippet is a simple python random password generator.
Firstly we import module random
that has the function to help us randomly pick data from a variable.
import random
An Integer (Int
) Input for password length; the most websites will ask for a minimum of 8-character passwords
length_of_password = int(input("How long do you want your password to be? "))
Declaring Variables for numbers, Symbols, and Alphabets(Lower and Upper case)
length_of_password = int(input("How long do you want your password to be? "))
lower_case = 'abcdefghijklmnopqrstuvwxyz'
uper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
symbols = '!@#$%^&*()_+,./?;:[]{}\|'
Then we add all the declared variables and house them in another variable use_for]
use_for = lower_case + uper_case + number + symbols
Here We house the length_of_password inside another variable password_length I believe someone might ask why to waste another variable if you can pass the variable directly into the code. Well, that's true. This code is flexible, and you can decide to remove the input line of code and pass an int value directly into password_length i.e password_length= 16
password_length = length_of_password
Here we will use the function random.sample
From our imported module, and then we will use“”.join
it to join our random data. Additionally, there is a link to random and join information.
password = "".join(random.sample(use_for, password_length))
Print Result
print("Your password has been generated \n", password)
import random
#Generate a random password
#Password length must be between 8 and 128 characters
print("\n"*2)
length_of_password = int(input("How long do you want your password to be? "))
lower_case = 'abcdefghijklmnopqrstuvwxyz'
uper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
symbols = '!@#$%^&*()_+,./?;:[]{}\|'
use_for = lower_case + uper_case + number + symbols
#Password length
password_length = length_of_password
password = "".join(random.sample(use_for, password_length))
print("\n"*2)
print("Your password has been generated \n", password)
print("\n"*2)