Create a random password generator program in Python

Create a random password generator program in Python

Hello World !

We make use of passwords in our daily life to prevent any hacker to gain access to our personal/confidential data. Thus it is vital to always make use of strong password.

Random Password Generator is a program which will help you to create a password mix of upper/lowercase alphabets as well as numbers & symbols to maintain paramount security.

What will be covered:

  1. What is Password?

  2. Random and string module

  3. How to create random password generator program

1. What is Password ?

A password, sometimes called a pass-code, is secret data, typically a string of characters, usually used to confirm a user's identity.

A password is an arbitrary string of characters including letters, digits, or other symbols.If you want to know more about password, refer to Password Wikipedia

2. Modules Used :

~String Module: string module contains constants, classes to process python string.

~ Random Module : random module is used to generate random sequence. We are using random.sample module here. If you will observe in the output all characters will be unique. random.sample() never repeats characters. If you don’t want to repeat characters or digits in the random string, then use random.sample() but it is less secure because it will reduce the probability of combinations because we are not allowing repetitive letters and digits

3. Time to code :

image.png

(a). In-order to access the python library, import the package.

import random
import string

(b). Start by greeting the user :

print("Hello, Welcome to random password generator program! .")

(c). Ask the user for length of password & store it in variable

length=int(input("\n Enter the length of password !"))

(d). Define the data with string module

lower=string.ascii_lowercase
upper=string.ascii_uppercase
digits=string.digits
punct=string.punctuation

(e). Concatenate all the data consisting of uppercase, lowercase as well as digits & numbers.

all=lower+upper+digits+punct

(f). With the use of random_sample module, letters generate the password by passing the all data and length of password.

temp=random.sample(all,length)

(g). Join the list into string and print the value

pass="".join(temp)
print(pass)

(h). After understanding the logic, we can reduce the lines of code

 all = string.ascii_letters + string.digits + string.punctuation
pass = "".join(random.sample(all,length))

With these steps, we have successfully created a random password generator using python. That's it!

I hope you find it helpful.

You can find the copy of program on my github : Samra-github

Please connect with me on my LinkedIn : Samra Qureshi