Password Generator Using Python

ยท

9 min read

Password Generator Using Python

Hi, hope you all are doing well. In this article, I will be showing how can we make our own password generator using Python. In my previous article, I've gone through how Python works under the hood, if you haven't read that and want to read it you can check it out here. Otherwise, let's move on and build our password generator.

But what is a password generator?

Well as the name suggests it is an application/program that will generate a new, strong and random password every time.

Okay... but why do I need a password generator?

Yeah, you probably won't need a password generator if you are great at creating new and strong passwords for your accounts on different websites. But if you are someone like me, who always struggles to think of a strong password, then this would be helpful for you. But nonetheless, you should make one because it would really help to learn some basic concepts of Python and you can share it with your friends or family who might need it.

Building Password Generator

So, now as we have understood the need for a password generator let's start building our own.

-Output we want

Let's first see what do we want as our end result so that we can start building our password generator.

  1. First of all we want to make this dynamic (something that is not fixed or pre-defined) so that the user can generate a password of any length. So, we will ask the user to enter the length of the password.
  2. Now that we have taken the user's input we would generate a random password (combination of numbers, symbols and alphabets) of the given length.
  3. It's the final and simplest step, display the generated password to the user.

-Requirements

The only requirement for this is the random module of python that we will be using to generate random characters every time (I will explain it later when we use it in our program). I know, I have written a lot without actually coding but believe me it's really important to have a clear idea of what you want and what are the requirements before we actually start coding. But that's it, now it's really time to start coding. So let's go...

Finally, let's code

I will divide the whole program into 4 sections so that I can easily make you understood everything.

STEP1:

import random

Now there are a lot of things we can do in python without additional help, but there are some things like in this case selecting/generating random results for which we require some additional help of some programs also called modules which are already created by developers for our ease. So the way we can use these modules is by importing them into our python program and the above line 'import random' is how we do it, import tells the python interpreter that we are going to import some additional module into our program and the next specified name (random in this case) is the name of the module that is being imported in our program. Now random module comes pre-installed with Python, but there are some modules that we might need to install explicitly (But more on that later).

STEP2:

small_alphabets = [chr(x) for x in range(65, 91)]
cap_alphabets = [chr(x) for x in range(97, 123)]
nums = [str(x) for x in range(10)]
symbols = ['~','!','@','#','$','%','^','&','*','(',')','_','+','=','-','?','/','>','.','<']
range(65, 91)it returns a range of numbers from 65 to 90
chr(x)it converts a given number into its equivalent character according to ASCII
str(x)it converts given number into string
[chr(x) for x in range(97, 123)]this is known as list comprehension it is an easy way to create a new list in python

You should read about all of these from the links provided below to have a better understanding.

range(), chr(), str(), list comprehension

STEP3:

length = int(input("length of password: "))
combination = small + caps + nums + symbols
random.shuffle(combination)
password = random.sample(combination, length)

In the first line, we are taking the length of the password as input and in python whatever input you take it's treated as a string. So, we convert it to an integer using int()

It will produce an error if you input any sort of character instead of a number

then we added all the lists we created above in STEP2 into one list

then, in the next line, we use our imported module "random". Whenever we want to use a function that's inside the imported module we need to use it this way (using dot operator) it specifies to the interpreter that this method, you need to find in this module. So the shuffle() method or function of the random module is used to well... shuffle the list into a random order.

And random.sample() takes a list and give us some random part of the specified length from the given list

STEP4:

print("Here's a strong Password for you")
print(''.join(password))

In the final step, we display the generated password to the user. ''.join() is used to join all the elements of the given list (password in this case) with '' (empty string in between). If we did something like '#'.join(password) and say our password list was ['p', 'a', '1', '$'] then the final result we get would be 'p#a#1#$'. I hope the example made it clear, if not you can refer to this link.

COMPLETE CODE

import random

small = [chr(x) for x in range(65, 91)]
caps = [chr(x) for x in range(97, 123)]
nums = list(map(str, [x for x in range(10)]))
symbols = ['~','!','@','#','$','%','^','&','*','(',')','_','+','=','-','?','/','>','.','<']

length = int(input("length of password: "))
combination = small + caps + nums + symbols
random.shuffle(combination)
password = random.sample(combination, length)

print("Here's a strong Password for you")
print(''.join(password))

OUTPUT

Screenshot (450).png

CONCLUSION

I hope this would have helped you in some way and this is how I built it, please try and make it in a new way read the official documentation, use google and try to find another way to make it even better. I will leave you with an idea to upgrade this and practice more python what you can do is ask the user how many alphabets, numbers and special characters are required in the password then accordingly try to create a strong random password.

BEST OF LUCK ๐Ÿ’ฏ

ย