Signup/Sign In
LAST UPDATED: FEBRUARY 23, 2022

Random Password Generator in Python Language

Random Password Generator in python language

In this tutorial, we'll build a password generator to enable us rapidly generate random and secure passwords.

What is the Purpose of a Password Generator?

We can't instantaneously come up with new password patterns.

Computers, on the other hand, are not the same. In a matter of seconds, computers can create random and strong passwords depending on our preferences. There are several password generators on the market.

Is it possible to make our own with the adjustments we want?

Yes, we can certainly put one together. And we'll teach you how to do it right now.

Let's start by making a password generator.

Generator of Passwords

The nicest part about making our own password generator is that we can make it exactly as we want it.

First, we'll make a password generator that asks for the password length and then produces a random password including numbers, alphabets, and special characters.

We'll next enhance it by asking how many of each sort of character there are, such as numerals, alphabets, and special characters.

So, without further ado, let's look at how to make a Python password generator.

Steps

  • Make a list of all the characters. We may type all of them or utilize Python's string module.
  • Request that the user provide the password length.
  • Using the random.shuffle technique, shuffle the characters.
  • To save the password, create an empty list.
  • Make a loop that iterates for the duration of time.
  • Using the random.choice technique, choose a random character from all the characters.
  • Toss in a random character at the end of the password.
  • To make the final password list more random, shuffle it.
  • Using the join technique, convert the password list to a string.
  • The password should be printed.

Try to write code using the procedures shown above. Don't worry if you can't write the code yourself. Take a look at the code below.

Basic Code

import string
import random


## characters to generate password from
characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")

def generate_random_password():
	## length of password from the user
	length = int(input("Enter password length: "))

	## shuffling the characters
	random.shuffle(characters)
	
	## picking random characters from the list
	password = []
	for i in range(length):
		password.append(random.choice(characters))

	## shuffling the resultant password
	random.shuffle(password)

	## converting the list to string
	## printing the list
	print("".join(password))



## invoking the function
generate_random_password()


The code in the example above is self-explanatory. To write code, we simply followed the procedures outlined. If you follow the procedures, you should have no trouble comprehending the code.

Run the code now to create a password. The following is an example of the output.

Enter password length: 10
d&amIzK%d)

Take note of the password in the output above. Is there a digit somewhere? No. Because there's no way of knowing whether or not the software will have digits.

Next, we ask the user to specify the amount of numbers, alphabets, and special characters they want, in order to ensure that the software will contain them.

The application will include the appropriate number of character kinds in the password when the user specifies the number of characters for each type.

Let's have a look at the code that takes the amount of characters for each category and creates the password.

Random Password Generator in Python Source Code

import string
import random


## characters to generate password from
alphabets = list(string.ascii_letters)
digits = list(string.digits)
special_characters = list("!@#$%^&*()")
characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")

def generate_random_password():
	## length of password from the user
	length = int(input("Enter password length: "))

	## number of character types
	alphabets_count = int(input("Enter alphabets count in password: "))
	digits_count = int(input("Enter digits count in password: "))
	special_characters_count = int(input("Enter special characters count in password: "))

	characters_count = alphabets_count + digits_count + special_characters_count

	## check the total length with characters sum count
	## print not valid if the sum is greater than length
	if characters_count > length:
		print("Characters total count is greater than the password length")
		return


	## initializing the password
	password = []
	
	## picking random alphabets
	for i in range(alphabets_count):
		password.append(random.choice(alphabets))


	## picking random digits
	for i in range(digits_count):
		password.append(random.choice(digits))


	## picking random alphabets
	for i in range(special_characters_count):
		password.append(random.choice(special_characters))


	## if the total characters count is less than the password length
	## add random characters to make it equal to the length
	if characters_count < length:
		random.shuffle(characters)
		for i in range(length - characters_count):
			password.append(random.choice(characters))


	## shuffling the resultant password
	random.shuffle(password)

	## converting the list to string
	## printing the list
	print("".join(password))



## invoking the function
generate_random_password()


So, what's the difference between this code and the one before it?

  • To add the characters to the password, we created separate loops for each kind of character.

  • There are two conditional checks to ensure that the total number of characters in the password matches the length of the password.

We've added some more code. The pattern, however, is identical to that of the prior code. As a result, you won't have any trouble comprehending it.

Now it's time to run the code and examine the results. Take a look at the following example of a test run.

Enter password length: 10
Enter alphabets count in password: 3
Enter digits count in password: 2
Enter special characters count in password: 3
V2(&#XlQq1

If you look at the password that was created this time, you'll see that it contains the minimal amount of characters that the user requested. In addition, the computer has added two extra random characters to make the password length equal to the length entered by the user.

Hurray! We offer a full-featured, secure password generator.

Conclusion

We've shown how to build a password generator from the ground up. Is it possible to add additional features to it? Yes, we are free to add as many as we like. However, be certain that the resulting password is sufficiently strong.

Make a password generator using the code and use it for your next online account.



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.