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 stringimport randomdef generateRandomPassword(length):digits = string.digitsletters = string.ascii_letterspunctuation = string.punctuationsamplepass = letters + digits +punctuation + punctuationpassword = "".join(random.sample(samplepass, length))return passwordif __name__ == "__main__":length = input("Enter the length ofthe Password Required!\n")try:length = int(length)password = generateRandomPassword(length)print("Password is : ", password)except:print("Please Enter Numerical Value.")exit(0)
-
Output:
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:
0 Comments
Please Let me Know, If you have any doubts.