How To Generate a Random Password Using Python

Let us see how to generate a random password in this article. Here we generate a strong password using a combination of alphabets, numbers, and symbols.

For this purpose we use the below modules in this project:

  • random module
  • string module

Python Program to Generate a Random Password

random module in Python:
The random module in Python can be used to generate random numbers.
These are pseudo-random numbers since the seed determines the sequence of numbers generated.
This module can be used to do tasks like generating random numbers, outputting a random value for a list or string, and so on.

string module:

The Python String module contains string manipulation constants, utility functions, and classes.

string.ascii_letters:
It represents all the letters(alphabets)
“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”

string.punctuation :
It represents all the symbols that are shown below
” !”#$%&'()*+,-./:;<=>?@[]^_`{|}~”

string.digits:
It represents all the digits(numbers)
“0123456789”

We concatenate all the above three methods and generate a random password.

random.choice(char):
It is used to select a random character from all the characters.

random.randint(6, 16):
It is used to generate a random integer in the given range.

Approach:

  • Import string module using the import keyword
  • Import random module using the import keyword
  • Concatenate all the alphabets, symbols, and digits using the string.ascii_letters, string. punctuation , string.digits to create a set of all possible characters and store it in a variable.
  • Generate a random password in a given range(6, 25) using the random.choice(), for loop, and random.randint() functions and convert it into a string using the join() function.
  • Print the randomly generated password.
  • The Exit of the Program.

Below is the Implementation:

# Import string module using the import keyword
import string
# Import random module using the import keyword
import random

# Concatenate all the alphabets, symbols and digits using the string.ascii_letters,
# string.punctuation ,string.digits to create a set of all possible characters
# and store it in a variable.
charcters = string.ascii_letters + string.punctuation + string.digits

# Generate a random password in a given range(6, 25) using the random.choice(), for loop,
# and random.randint() functions and convert it into a string using the join() function.
rndm_password = "".join(random.choice(charcters) for i in range(random.randint(6, 25)))
# Print the randomly genarated password.
print('Random Password : ',rndm_password)

Output:

Random Password : [FkdR]6dTw4#XSfx_

Explanation:

Now you must construct a password that is both random and strong. We’ll make it a random length, with a random selection of characters. Using random. choice, we select characters at random (char). This random selection occurs 6 – 25 times, resulting in a password length of 6 to 25 characters. This random length is determined by random. randint(6, 25), which returns a random integer within the given range.

We create an empty string using “” and then use. join to add this string of random characters to it, resulting in a string password. Using a for loop, we add characters one by one. This loop executes the value returned by the randint() function.

We now use print to display the password (random password).