0% found this document useful (0 votes)
27 views10 pages

PWP Mic

This document discusses building a random password generator in Python. It describes importing modules, defining a function to generate passwords from alphanumeric characters, updating the program to accept the password length from the user, and updating it further to generate multiple passwords.

Uploaded by

nikitadhere2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

PWP Mic

This document discusses building a random password generator in Python. It describes importing modules, defining a function to generate passwords from alphanumeric characters, updating the program to accept the password length from the user, and updating it further to generate multiple passwords.

Uploaded by

nikitadhere2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Random password generator

INTRODUCTION

we will build a Random Password Generator in Python. The "Random Password Generator" is a
program that will generate strong random passwords of the specified length with the help of the
alphabet, numbers, and symbols. Let us first understand the prerequisites and build the intuition
of the program so that the implementation can become simpler.
But before we start, a good knowledge of Python programming language is necessary, along with
a clear understanding of the random module and data structures to build a random password
generator in Python.
Passwords are a means by which a user proves that they are authorized to use a device. It is
important that passwords must be long and complex. It should contain at least more than ten
characters with a combination of characters such as percent (%), commas(,), and parentheses,
as well as lower-case and upper-case alphabets and numbers. Here we will create a random
password using Python code.

Computer technology Page 1


Random password generator

 Steps to Build the Random Password Generator in Python:-


The following are the steps to implement the random password generator in Python:
Step 1: Firstly, we will import the required modules.
Step 2: We will define a function to generate a password using all alphanumeric characters.
Step 3: We will update the program to accept the password length as the user input.
Step 4: We will update the program again to generate multiple passwords.
Step 5: Finally, we will integrate Graphical User Interface (GUI) in the Password Generator
using Tkinter.
Let us now understand the implementation of the above steps in detail.

Computer technology Page 2


Random password generator

 Defining a Function to Generate the Password using All Alphanumeric


Characters:-
Now that we have imported all the necessary modules in the program, it is time to start defining
a function that will allow us to generate the password with the help of all alphanumeric
characters.
Step 1: We will start by defining a function that will return a randomly generated password. This
function will accept one parameter signifying the length of the password to be generated.
Step 2: We will create a string or list containing all the alphabets (small and capital letters),
numbers, and symbols.
Step 3: We will initialize an empty string to store the randomly chosen password.
Step 4: At last, we will run a loop iterating from 0 to the length of the password. We will
randomly select a character from the character list with the help of the random.choice() method
and add the selected character to the password string. Finally, we will return the password string.
Let us consider the following snippet of code illustrating the same.

EXAMPLE:-
import random

def generate_password(len):

"This function accepts a parameter 'len' and returns a randomly generated password"

list_of_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^
&*()"

pass_str = ""

for i in range(len):

pass_str = pass_str + random.choice(list_of_chars)

return pass_str

if __name__ == "__main__":

len = 8

pass_str = generate_password(len)

print("A randomly generated Password is:", pass_str)

Computer technology Page 3


Random password generator

Output:-

Computer technology Page 4


Random password generator

 Updating the Program to accept the Password Length from the User:-
Now that we have defined the function, we need to generate a password of a given length, it is
time for us to make one adjustment to the program. As observed in the previous program, we
have explicitly added the length of the required password. We can also accept the user's
password length by using the input() method in Python.
Let us consider the following snippet of code illustrating the same.
EXAMPLE :-
import random

def generate_password(len):

"This function accepts a parameter 'len' and returns a randomly generated password"

list_of_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^
&*()"

selected_char = random.sample(list_of_chars, len)

pass_str = "".join(selected_char)

return pass_str

if __name__ == "__main__":

len = int(input("Enter the length of the Password: "))

pass_str = generate_password(len)

print("A randomly generated Password is:", pass_str)

Computer technology Page 5


Random password generator

Output:-

Computer technology Page 6


Random password generator

 Updating the Program to Generate Multiple Passwords:-


We will now update the program once again to generate multiple passwords using the same logic
as discussed earlier.
To generate more than one random password, we can add an infinite loop to the program that
will accept the length of the required password and produce the password. We can ask a simple
question asking the user whether they want to generate a password or exit the program.
Let us consider the following snippet of code demonstrating the same.

EXAMPLE :-
import random

def generate_password(len):

"This function accepts a parameter 'len' and returns a randomly generated password"

list_of_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^
&*()"

selected_char = random.sample(list_of_chars, len)

pass_str = "".join(selected_char)

return pass_str

if __name__ == "__main__":

while True:

userSelection = input("Do you wish to generate a Password?\nPress 'Y/y' to Continue, or


'N/n' to Exit: ")

if userSelection == 'N' or userSelection == 'n':

print("Thank You! See you next time.")

break

elif userSelection == 'Y' or userSelection == 'y':

len = int(input("Enter the length of the Password: "))

pass_str = generate_password(len)

Computer technology Page 7


Random password generator

print("A randomly generated Password is:", pass_str)

print("")

else:

print("Invalid Input! Try Again.")

print("")

Output:-

Computer technology Page 8


Random password generator

CONCLUSION

o The Random Password Generator is a program that is able to generate strong random
passwords of the specified length with the combination of alphabets, numbers, and
symbols.

o In this tutorial, we have learned different ways of developing the Random Password
Generator using Python as the programming language.

o We have also learned the use of different methods of the random

o We have also observed the use of the Tkinter module to create a Graphical User Interface
(GUI) for the Random Password Generator.

o At last, we have discussed the reason for creating the Random Password Generator and
how strong passwords can be generated using this application.

Computer technology Page 9


Random password generator

REFERENCE
https://www.javatpoint.com/java-password-generator

https://www.javatpoint.com/random-password-generator-in-python

https://www.dashlane.com/features/password-generator

Computer technology Page 10

You might also like