Latest Tech

Recent Posts: Label / Tag Ex:

Easy Python Password Generator


It’s very important to secure your accounts with reliable passwords. Choosing the right password could be a challenge for some. Today, we are going to make an easy program with Python to generate random passwords.

At first, let me introduce you to have-I-been-pwned?. This unique site enables you to check whether your email or username has been pwned (compromised) in a data breach. Thousands of passwords were leaked over the past few years, let’s be more careful, shall we?



Another awesome tool to check your password strength and have a rough idea how long it would take a computer to crack your password, it’s called HOW-SECURE-IS-MY-PASSWORD?. I personally use this tool whenever I’m setting up a new account.


Now, let's get to business. Passwords contain all kind of characters, so we create a list that will be stored and used to generate the password.


 This list contains all the possible characters 
char = 'abcdefghijklmnopqrstuvwx123456789`~!@#$%^&*()_+|/' 

Next, in order to make the program to choose randomly, we import ‘random’.
The code will be like this:

mport random

# This list contains all the possible characters 
char = 'abcdefghijklmnopqrstuvwx123456789`~!@#$%^&*()_+|/' 

Next, we make use of the module ‘random’ to pick random characters. However, the program will produce only one character

password = random.choice(char)

So far the code is:


import random

# This list contains all the possible characters 
char = 'abcdefghijklmnopqrstuvwx123456789`~!@#$%^&*()_+|/' 

# Empty password variable  
password = ''

# To pick random characters from the list ‘char’    
password = random.choice(char)

print (password)

The output is

k

In order to make the program generate multiple characters at once, we put a condition and empty ‘password’ variable. Let’s not forget to indent the line Password = random.choice(char). Also, lets not forget to add ‘+=’ in the line Password += random.choice(char).

The code will be like this

import random

# This list contains all the possible characters 
char = 'abcdefghijklmnopqrstuvwx123456789`~!@#$%^&*()_+|/' 

# Empty password variable  
password = ''
# The condition to generate multiple characters at once  
for x in range (5):

# To pick random characters from the list ‘char’    
 password += random.choice(char)

print (password)

The output generates 5 characters at once:

iacbi

Let's make it more convenient by creating user input instead of changing the code every time we pick different range. We simply add

# Create user input to decide the password length 
length = input(" what is the length of your password")
length = int(length)

The code will be like this:

import random

# This list contains all the possible characters 
char = 'abcdefghijklmnopqrstuvwx123456789`~!@#$%^&*()_+|/' 

# Create user input to decide the password length 
length = input(" what is the length of your password?")
length = int(length)

# Empty password variable  
password = ''
# The condition to generate multiple characters at once  
for x in range (length):

# To pick random characters from the list ‘char’    
 password += random.choice(char)

print (password)

The output will ask the user then generate a random password:

what is the length of your password? '8'
ioehwjpo

Now, let’s add more feature such as creating passwords list. To do so, we create a new condition


passList = input("How many passwords would you like to generate?")
passList = int(passList)

# The condition to create passwords list
for list in range(passList):

we add these simple lines to the main code and the final piece should look like this:  

import random

# This list contains all the possible characters 
char = 'abcdefghijklmnopqrstuvwx123456789`~!@#$%^&*()_+|/' 

# Create user input to decide the password length 
length = input(" what is the length of your password?")
length = int(length)

# To create user input to decide the num of passowrds list
passList = input("How many passwords would you like to generate?")
passList = int(passList)

# The condition to create passwords list
for list in range(passList):

# Empty password variable  
 password = ''
# The condition to generate multiple characters at once  
 for x in range (length):

# To pick random characters from the list ‘char’    
   password += random.choice(char)

 print (password)


The final output should be some thing like this

What is the length of your password? '7'
How many passwords would you like to generate? '10'

%|dj_da
m(gi#o_
4utg8+_
6`*o8*~
(f!(s`k
8c~jdxj
db1(~&!
o|qfqu(
9h6g5b`
nt)47qx

Another output

What is the length of your password? '25'
How many passwords would you like to generate? '4'

sr8!x+d8d%fhw3|r($~6%4pxp
^1&wiu+sr_xb8vri|#r57a`of
)owd53ac+x~/e+@)n9i3@jss1

#xru($43e`/hf*%pu@m9`!c|q

No comments