How to Create Strong Random Password Generator In Python

Codeityweb
2 min readJan 9, 2021

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 in python
Random Password With Length 20
Strong Random password generator in python with length 15
Random Password With Length 15
Password Length Should be a Number
Length Should be a Number

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.

--

--

Codeityweb

A Website To Learn Coding,Get Mini Projects With Implementations ,Competitive Programming ,HackerRank Solutions.@ https://codeityweb.blogspot.com