How to Create a Random Password Generator in Python

By Rahul Kumar Sharma in Development on July 26, 2022

Security is one of the most pivotal corridors of our lives. The significance of security is adding day by day as the most effects are going online. Watchwords come into light as we talk about security.  In this composition, we will produce a word creator that helps us induce arbitrary and strong watchwords snappily. 

 Why do we need a word creator? 

 There is no way to think of different patterns of watchwords incontinently since we can’t think of them all. But, it isn’t the same case with computers. Computers can induce arbitrary and strong watchwords grounded on our customizations in seconds. There are numerous word creators available. 

 Can we produce our own with the customizations we like? 

 Yes, we can surely produce one. And then we’re going to show you how to do that. 

Let’s produce a word creator. 

 Word creator 💻 

  •  The stylish thing about creating our word creator is that we can customize it as we like. 
  •  First, we will produce a word creator that asks the length of the word and generates an arbitrary word containing integers, rudiments, and special characters. 
  •  Next, we will ameliorate it by asking the number of each type of character, like the number of integers, rudiments, and special characters. 
  •  So, without further ado, let’s see the way to produce a word creator using Python. 

 Steps 

  1.  Store all the features as a list.
  2.  Ask the stoner to enter the length of the word. 
  3.  Shuffle the characters using the random. shuffle system. 
  4.  Write a circle that iterates length times. 
  5.  Pick an arbitrary character from all the characters using the random. choice system. 
  6.  Tack the arbitrary character to the word. 
  7.  Shuffle the attendant word list to make it more arbitrary. 
  8.  Convert the word list to string using the join system. 
  9.  publish the word. 
See also  Introduction to Windows Server Administration

 Follow the below way and try to write law. Don’t worry if you aren’t suitable to write the law. Check out the law below.

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 below law is tone-explicatory. We’ve just followed the way described to write law. So, you won’t find any problem in understanding the law if you read the way.

Now, run the law and induce a word. An illustration affair will be as follows.

Enter password length: 10
8Db@uVxHf4
Process finished with exit code 0

Observe the word in the below affair. Is there any number? No. Because there’s no guarantee that the program will include integers. Next, we try to guarantee that the program will include integers and special characters by asking the stoner to enter the number of integers, rudiments, and special characters they want. When the stoner enters the number of characters for each type, the program will include a separate number of character types into the word.

Let’s see the law that accepts the number of characters for each type and generates the word.

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 the former law and this law? 

See also  Become a Google Certified Data Scientist for Free

 We’ve written separate circles for each type of character to add them to the word.  The total character count with word length can be validated using two tentative checks. We’ve added some redundant laws. But, the pattern is analogous to the former law. Now, it’s time to execute and check the affair. Look at the following 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
8uZD^)(c&5

Process finished with exit code 0

Still, it has the minimal number of characters that the stoner wants If you see the word generated this time. And the program has included 2 further arbitrary characters to make the word length equal to stoner input.

Hurray! we have got a completely strong word creator. 😎

Conclusion

We’ve seen how to produce a word creator from scrape. Can we add further features to it? Yeah, we can add as numerous as we want. But, make sure that the attendant word is strong enough. Induce the watchwords from the law and use them for your coming online account.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.