How to Create Random Password Generator In Python | Codeityweb


Creating Strong Random Password Generator In Python


In this post we will create a Random Password Generator Script in Python

For that we will use string and random module present in python

So Without any further discussion lets create the script .

First we will import the required modules, we don't have to install them they already come with python.


Code:

-

import string
import random
 
def generateRandomPassword(length):
 
    digits = string.digits
    letters = string.ascii_letters
    punctuation = string.punctuation
    samplepass = letters + digits + 
                            punctuation + punctuation
    password = "".join(random.sample(samplepass, length))
    return password
    
if __name__ == "__main__":
    length = input("Enter the length of
     the Password Required!\n")
    try:
        length = int(length)
        password = generateRandomPassword(length)
        print("Password is : ", password)
    except:
        print("Please Enter Numerical Value.")
        exit(0)



-





Output:



Random Password generator python



Random password generator python



Random password generator python




Explanation:

string.digits
= 0123456789

string.letters = abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

string.punctuation = !"#$%&'()*+,-./: ;<=>?@[\]^_`{|}~

Note : Here we used punctuation 2 times just to increase  the sample size and increase the chances of picking more punctuation marks in the password

We use random.sample() function to randomly select the characters from the sample space.

 

Related Posts:

Post a Comment

0 Comments