Random Password Generator using Python Programming

Python Project for Beginners and Intermediates

Rohit Kumar Thakur
Python in Plain English
3 min readSep 19, 2021

--

Photo by Hitesh Choudhary on Unsplash

Password is a major concern these days. You need strong encryption in this crazy virtual world. When you make an account on any website, they often ask you to write a strong password. In the password, there should be digits, upper case alphabets, lower case alphabets, special characters, and so on. A hacker can’t easily crack a strong password.

I know it’s hard to think of a new password each time for a new website. Because the password should be strong enough and fulfill all the criteria. Don’t worry, the Python programming language has got your back. With the help of this programming language, you can create a unique password each time. Let’s get started.

Well, you don’t need any package installation here. All you need is a text editor. I prefer VS code. What about you?

The video tutorial of this article is here. In this video, I used the python secret library.

Code of Password Generator using python

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

Make a Python file. Name it as per your choice. I named it password.py

import randompassword_len = int(input("Enter the length of the password: "))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']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']DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']SPECIAL = ['@', '#', '$', '=', ':', '?', '.', '/', '|', '~', '>', '*', '<']COMBINED_LIST = DIGITS + UPPERCASE + LOWERCASE + SPECIALpassword = "".join(random.sample(COMBINED_LIST, password_len))print(password)
  • Import the library
  • Ask for the length of the password needed.
  • Declare arrays of the character that we need in our password. We need upper case characters, lower case characters, digits, and special characters. All of these are represented as chars to enable easy string concatenation.
  • Combines all the character arrays above to form one array.
  • We used random.sample() function. It will take random characters from the COMBINED_LIST variable of length password_length. We also used the join() function which will join our generated password to an empty string on the left.
  • Finally, print the value.

We are done here. Now, let’s run this code. Open your terminal or command prompt window in the code directory and run the following command:

python password.py

or

python3 password.py
Password generator using python

Each time we have a unique password, as you can see that in the image above. Now, save that password in your secret diary.

Well, That’s it.

If this article sounds informative then make sure to follow and share it with your geek community.

Here are more Python projects for practice!

Make a Calculator using Python and Tkinter

YouTube Video Downloader using Python and Tkinter

Text to Speech Conversion using Python with gTTS

Capture and Process Video Footage from a Webcam using OpenCV Python

Rock, Paper, and Scissors Game using Python Programming

Happy coding..!!

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build react native projects and currently working on Python Django. Feel free to contact me (freelance.rohit7@gmail.com)

--

--