Generate Random Password using Python
By: Jitendra Singh Balla
Tags: Coding, Python, Programming, Software Development, Code

Photo by Markus Spiske on Unsplash

Password can be define as ‘Secret word/words’ or may be a number. In simple words, it is a key to a lock. Google also suggests us randomly generated password. The purpose of this article is that know about how to generate password using Python language.

Requirements:

Nothing special requirements but you must have Python installed on your PC and have a little bit knowledge of computer programming. If you don’t have than no need to worry you can simply download it by clicking on Python.

First of all, import random and string package.

import random, string

than creates a function called password and we’ll write our randomly generated password code into it. Now pass the arguments into such as how much digit we want into password, length of the password and strength of the password. As the below code shows with docstring

def password(length, num=false,strength='weak'):

"""length of password, num if you want a number and strength(weak, strong,very)"""

The above git has whole code into it. So now I’ll explain it line by line.

#From line 5 to 10

lower = string.ascii_lowercase

upper = string.ascii_uppercase

letter = lower + upper

dig = string.digits

punct = string.punctuation

pwd = ' '

From line 5 to 10, we’re just creating variables for lowercase letters, uppercase letters, letters, digits, punctuation and password named lower , upper , letter, dig , punct , pwd respectively.

The letter variable consists combination of lowercase letters and uppercase letters which are going to be use in password. The string.ascii_lowercase and string.ascii_uppercase takes the ASCII value of the lowercase letters and uppercase letters. The string.punctuation function is used for making password clear from separators. The string.digits function is used for taking digits in string format.

if strength == 'weak':

if num :

length -= 2

for i in range(2):

pwd += random.choice(dig)

for i in range(length):

pwd += random.choice(lower)

From line 11 to 17, we’ve written conditional code. The condition is for ‘weak password’. In line 11, If we choose ‘weak password’ to use than this condition will run. In line 12, If num is True than the program will include 2 digits because we’re taking length variable equals to 2 for same condition. In line 14 and 15, the for loop is adding two digits one by one into password. In line 16 and 17 , again the for loop is adding lowercase letters in password.

elif strength == 'strong':

if num:

length -= 2

for i in range(2):

pwd += random.choice(dig)

for i in range(length):

pwd += random.choice(letter)

From line 19 to 17, we’ve written conditional code again. The condition is for ‘strong password’. In line 19, If we choose ‘strong password’ to use than this condition will run. In line 20, If num is True than the program will include 2 digits because we’re taking length variable equals to 2 for same condition. In line 22 and 23, the for loop is adding two digits one by one into password. In line 24 and 25 , again the for loop is adding letters which may be lowercase or uppercase in password instead of just lowercase as we’ve used for ‘weak password’ condition.

elif strength == 'very strong':

ran = random.randint((2,4)

if num:

length -= ran

for i in range(ran):

pwd += random.choice(dig)

length -= ran

for i in range(ran):

pwd = random.choice(punct)

for i in range(length):

pwd = random.choice(letter)

From line 26 to 36, we’ve written conditional code again. The condition is for ‘very strong password’. In line 26, If we choose ‘very strong password’ to use than this condition will run. In line 27, we’re using ran variable which is generating two to four digits randomly by using random.randint function.

In line 28, If num is True than the program will include 2 to 4 randomly generated digits because we’re taking length variable equals to ran in line 29 for same condition.

In line 30 and 31, the for loop is adding 2 to 4 randomly generated digits one by one into password. In line 33 and 34, the for loop is using punctuation. In line 35 and 36, which may be lowercase or uppercase in password instead of just lowercase as we’ve used for ‘weak password’ condition.

pwd = list(pwd)

random.shuffle(pwd)

return ''.join(pwd)

In line 38, we’re converting password into a list data type. In line 39, we’re shuffling letters and digits randomly. In line 40, all the keywords(letters and digits) are joined together by removing separators i.e. ‘ ’, “ ”,/ etc.

print(password(5, num=True))

print(password(10, num=True, strength = 'strong')) print(password(13, num=True, strength = 'very strong'))

In above lines, we’re calling function into action to perform various password condition that we coded.