Random Password Generator in Python

Ajay Choudhury 06 Aug, 2021 4 mins
Random Password Generator in Python

Almost all digital services and software have moved to the cloud and web nowadays and access to those requires an account from the user side. So a user who is active on the internet will obviously have a bunch of accounts over different sites. Creating different accounts on various sites require creating strong passwords for all of them and it is quite difficult to remember all of them. You can obviously use any good password managers for that. But if you want to create a random password generator of your own using your Python programming skills that can generate you some random and extremely difficult to guess passwords.

Here, we will create a simple program that can generate random passwords of length 12 to 100 characters based on your input every time. You will be amazed to see how huge the set of possible passwords is.

Let us begin with the Python program to build a simple yet powerful password generator. First of all, you will require to declare all the sets of characters you want to use to generate passwords. We will use all the digits, lowercase characters, uppercase characters and some special symbols to generate random and shuffled passwords out of them. Now, to pull random characters of the set we will be using the random module of Python.

import random

# define the set of characters we want to use
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
             'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
             'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

special = ['@', '#', '$', '%', '=', ':', '?', '.', '/',
           '|', '~', '>', '*', '(', ')', '<', '•', 'α', 'ß', 'γ']

Now, we have the sets of characters that can be used individually, but we want to generate a password that contains random characters from all the sets combined. So we will generate a superset combining all the individual sets.

collection = digits + uppercase + lowercase + special

We will also take user input to decide the number of characters the user wants in his/her password. By default user input is accepted as a string in Python, so we will typecast the input to the integer type.

# User input for number of characters
print('Enter the number of characters (12-100): ')
strn = int(input())

According to various data sources and statistics observed the optimal lengths of strong passwords are 12 to 15 characters. So we will design our password generator to generate passwords with a length of at least 12 characters and at most 100 characters (it might sound very big but there are some cases when we need big secret keys like the Secret Key in Django projects). To achieve that we will limit the user input such that if the user gives an input below 12 then the length will be raised to 12 and if the input is greater than 100 then the length will be fixed to 100. Otherwise, if the input is between 12 and 100 then it is taken as it is.

# limit the number of characters between 12 to 100
if strn < 12:
    strn = 12

elif strn > 100:
    strn = 100

Now, we will ensure that the password contains at least a character from each of the character set we defined above such that the password looks random in every possible case. To achieve that we will pull out a random character from each of the character sets and append them to an empty list. You can definitely shorten these by directly appending the random characters, I have declared variables to enhance readability.

# Take at least one letter from each character set randomly
dig = random.choice(digits)
low = random.choice(lowercase)
up = random.choice(uppercase)
sym = random.choice(special)

# append the above characters in an empty list
rnd_char = []
rnd_char.append(dig)
rnd_char.append(low)
rnd_char.append(up)
rnd_char.append(sym)

By this point, we have already created a list of 4 random characters but their order is still predictable as we have a digit first followed by a lowercase character which then was followed by an uppercase character and a special symbol, we will address this issue below. Now, we will move to the next part to generate the rest of the characters of the password randomly.

To get this done, we will run a for loop (length - 4) number of times as we have already taken the 4 characters above. Inside the for loop, we will fetch a random character from the superset of all the characters and append it to the same list into which we appended the previous 4 characters.

# apart from above 4 characters fill rest randomly and append to list
for i in range(strn-4):
    rnd_char.append(random.choice(collection))

At this point, we have a randomly generated list of characters with us, but as mentioned above the order of the first 4 characters is fixed and also we want a string not a list of characters as a password. So, we will shuffle the list of characters randomly using the shuffle() function from the random module and then we will declare an empty string into which we will append each character of the shuffled list to generate a random password as a string. At last, we will print the password.

# shuffle the final list of characters
random.shuffle(rnd_char)

# generate the password as a string
password = ""
for j in rnd_char:
    password = password + j

# print the password
print(password)

So, we have created a random password and key generator purely in Python. You can use it to generate passwords for your online accounts. You can see it working here. You can also customise the code according to your need and choice.

Was this Article Helpful?

Ajay Choudhury

Ajay Choudhury

A learner and a student of technlogy. He loves sharing experiences and learning with others through his projects and blog. Along with technology, he loves playing football and enjoys listening to music and podcasts.

0 Comments

Info

Contribute to make this site more awesome →