Generating a password is an important requirement and is a part of every application nowadays where the user is given an option to generate a secure password.
This auto generated password is a combination of lower and upper case alphabets, numbers and special characters.
In this article, we will look at 2 different methods to generate a random password of a fixed length.

Method 1: Using random.sample
This method makes use of sample() function from random module.
sample() method accepts a string and a number and returns a list of random characters picked from the string supplied as argument.
Length of the list is the same that is provided as the second argument to sample(). This list can be converted to a string using a join() function.

Thus, if we provide a string of upper and lower case letters, numbers and special characters to sample() function and a fixed length, then it will generate a list of random characters which can be joined to form random password.

Elements of the list returned by sample() are different, everytime it is invoked. Example program is given below

import random

# super set of password characters
charset = 'abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?'
# length of password
password_length = 8
# loop 5 times to generate different passwords
for i in range(5):
    # pick 8 random characters from the charset
    password_chars = random.sample(charset, password_length)
    # combine them into a string 
    password = ''.join(password_chars)
    print('Random password is', password)

Note that we are invoking sample() inside a for loop for 5 times. This is just to show that it is generating different passwords everytime.
Output of this program is

Random password is f#*4OzPT
Random password is oG0EMHFg
Random password is eyJjb&O$
Random password is ptyKWbsM
Random password is 6e3Wr4ZM

Method 2: Using random.choice and string module
Python’s random module has a choice() function which takes a string argument and returns a random character from this string.
So, if we invoke choice() for the number of times equal to the length of the password and combine the characters returned, we will generate a password.
Also, instead of writing the string of letters from which choice() will pick a letter directly into the code, we may use string module.
string module has separate strings for lowercase, uppercase letters, numbers and special characters as shown in the example below.

import random
import string

# define characters that the password may contain
charset = string.ascii_lowercase + string.ascii_uppercase + 
string.punctuation + string.digits
password_length = 8
password = ''
# loop till the password length
for x in range(password_length):
    # combine characters returned by choice()
    password += ''.join(random.choice(charset))

print('Password is', password)

This prints

Password is ni@>cK.j

Instead of using a loop to generate and combine characters to form the password, we can use a short hand syntax using generator expression.
join() function accepts an iterable(that can be iterated) as argument.
Generator expression returns an iterable and it could be supplied as argument to join() as shown below.

password = ''.join(random.choice(s) for x in range(password_length))

Both the methods shown in this article can be customized by taking user input such as the length of password, which characters need to be excluded while password creation etc. This is left as an exercise to the readers.
Hope this article was useful.

0
Liked the article ? Spread the word...