In this era, having strong passwords is essential to protect yourself from online attacks. While some find it burdening, Let's take this as a challenge to develop a solution by coding in Python!
In this guide, we will design a simple code in Python to generate an unpredictable and random password that contains alphabets, numbers, and symbols. we will also look at some basic applications of concepts in Python such as arrays, lists, variable declarations, and methods. So, let's start coding!
First, Let's import modules from the standard Python library for this project. Since components (such as upper case letters, symbols, digits, etc) of the password are stored in the form of an array, we need to import the array module. The array module allows you to create an array with a data type and value list specified in its arguments. The next module we need to import is the random module. This module is a built-in module to generate random elements from a list.
import random import array
Next, Let's set the maximum length of your password which consists of alphabets, symbols, digits. Here, I have created a variable named maxlen to store the length. You can also vary the length according to your requirements.
After setting the value of the maximum length of your password, let's define the elements which will be in your array. Let's declare the arrays of the characters that we need in our password. Characters are easy to concatenate.
# define the length of your password required at maximum capacity. maxlen = 8 # characters, symbols and digits that are required for the password are denoted in the arrays below. upchars = ['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'] lowchars = ['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'] symbs = ['*', '@', '$', '%', '_', ':', '.', '/', '^', '~', '>', '#', '(', ')', '<'] dig = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
A password contains all different type of characters together. So, let's combine all the character arrays into one, and store it in a variable called combo. Since, we need the program to randomly select elements from the array, we can use the choice() method. As the name suggests, the choice() method return a randomly selected element from a given sequence. Here, we can ensure that we select at least 1 character from each array randomly.
# let's combine all the character arrays we made into a single array combo = dig + upchars + lowchars + symbs # Let's make sure we randomly select (at least) one character from each arrays from above randdigs = random.choice(dig) randupchars = random.choice(upchars) randlowchars = random.choice(lowchars) randsymbs = random.choice(symbs)
next, Let's add up the elements which are randomly selected from above and store it in a variable called temp. Now that we have ensured that we have at least 1 character from each component, we can fill the rest of the password by randomly selecting from the combined list of characters ( named as combo) above. This can be achieved by looping.
# Let's add them temp = randdigs + randupchars + randlowchars + randsymbs # now, we are sure we have at least one character from each for x in range(maxlen - 4): temp = temp + random.choice(combo)
Let's change the temporary password into array and mix it up using the shuffle() method in Python. Using the shuffle() method will avoid the password from having a uniform pattern.
# convert temporary password into array templist = array.array('u', temp) random.shuffle(templist)
Now, Let's iterate the array of temporary password. Iterating or traversing is a process where the code goes through elements one by one. This can be achieved using basic loops. We can also append the same to get a password.
# traverse and append pswd = "" for x in templist: pswd = pswd + x # print out password print(pswd)
And on to our last step, let's print out the password and check the output!
Submitted by KHYATI MADDALI (khyatimaddali)
Download packets of source code on Coders Packet