Random Password Generator in Python Projects With Source Code

In this tutorial, I will show you how Python generates random String and Password with Source code and Examples.

For the demonstration of this random password generator in Python, I will use Python 3.8.2 version and Pycharm for my IDE.

By the way, if you are new to Python programming and don’t know what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you. I also have here How to Download and Install the Latest Version of Python on Windows.

Before we proceed, watch the video tutorial here.

A step-by-step guide on how to setup a Random Password Generator in Python Projects

Python Generate Random String and Password

  1. Step 1: Download Python 3.8.2

    Go to the official website of Python and click the Download Python 3.8.2 button as show below.
    download python

  2. Step 2: Install Python on your computer

    From the download folder, look at the downloaded Python installer and right-click, then select “run as administrator” and follow the instructions given during the installation.

  3. Step 3: Test Python

    To test Python if it is successfully installed on your computer, open the command prompt or press “Window + r” from your keyboard. then type cmd. When the command prompt opens.

    Type “python –version”. see the image below.

    python installed

  4. Step 4: Download and Install IDE

    Here’s the download window of Pycharm. Then click the “Download” button under Community. After downloading, look for the downloaded file and install it on your computer.

  5. Step 5: Create a new Project in Pycharm

    Open Pycharm and click File->New Project.
    Then specify the location where you store your project. In my case, I will use “itscProjects”.
    See the image below.

    python Create Project

  6. Step 6: Create Python File

    Right-click “Project”->Click “New”->Select “Python File”. See the image below for reference.create python file

  7. Step 7: Complete the creation of the Python File

    In the New Python File window, Type “randompass” and press enter.
    python file

  8. Step 8: Start coding

    Add the following code:
    Note: I will add an embeddable code below so that you can copy and paste it into your editor.random password in python

  9. Step 9: Display Result from Generated Random Password in Python

    when you execute the code in Step 8: you will have a result like as shown in the image. result of generated random password in python

Here’s the Code:

You can copy the code here and paste it into your code editor.

from random import *
mixchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@£$%^&*().,?0123456789"
randompass = "".join(choice(mixchars) for x in range(randint(8, 20)))
print("Your Random Password is: ", randompass)

Code Explanation

Let’s examine the code above.

In the first line of our code, we have a random module that is used to provide access to functions that support many operations.

In the second line, we use “mixchars” variable to hold some characters.

The random module in Python is really useful especially when you want to create a secure password, it can allow you also to pick a random number in a given range.

Just like what we did in the third line of code. We use randint() the functions of a random module to generate a random integer within a range.

and on the last line of code for the random password generator, we display the Generated Random password.

Upgrade Version of our Previous Codes

At this time we will explore more on another way of creating a random password generator in Python. To start with, Let us try the following code first.

import string

When you run the code above, it will give you a result that looks like as shown in the image below.

This is the result when you execute the code above.

As you have noticed, we have four(4) lines of code here. And I will explain each line.

Line 1: In this line, we use a Python string module that contains a number of useful constants and classes that are useful in our Python project to create a random password.

Line 2: In this line of code, we just simply display the list of ASCII letters both in a lower and upper case format.

Line 3: In this line of code, we just simply display the list of ASCII digits.

Line 4: In this line of code, we just simply display the list of ASCII punctuation.

At this time, we will try to combine all of the codes from our previous version of the Random password generator in Python. So here’s the following updated code.

import string
from random import *
mixchars = string.ascii_letters+string.digits+string.punctuation
randompass = "".join(choice(mixchars) for x in range(randint(8, 20)))
print("Your Random Password is: ", randompass)

You can observe in the code above that we have updated the value of “mixchars” variable from this:

to this:

So what do you think? Which is better to use? Please let me know by simply dropping your comment below.

Python Generate Random String

In this topic, we will create a Python application to generate a random string character.

Let’s add the following code in your editor.

import random
import string

def randStr(strlen):
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(strlen))

print("First Random String is ", randStr(8))
print("second Random String is ", randStr(8))

What is new to this code, is that we use a function named “randStr” that this function will return a result later code.

Next, we also use the “letters” variable to store the value from string ASCII letters.

In the return, you will also notice that we use random.choice() to pick up a single character from the string constant.

Then, at the end of the iteration, it adds to the string variable using the join function.

Finally, we display the value from the function. See the image below.

The output of Python generate random string

Python Generate Random String Without Repeating Characters

In our previous topic in Python generate random strings, we use there random.choice() to pick up a single character from the string constant.

But take note random.choice() might pick up the same character again. So to eliminate this issue, we will use random.sample() function.

See the code below:

import random
import string

def randStr(strlen=8):
letters = string.ascii_letters
return ''.join(random.sample(letters, strlen))

print("First Random String is ", randStr())
print("second Random String is ", randStr(8))

the result of the code will be.

Generate a Random Password in Python that accepts user input

At this time, we are going to generate a random password in Python that accepts user inputs. For instance, when the program runs, it will ask the user to input many numbers of passwords and the length of passwords to be generated.

So let’s start.

For this demonstration, I will be using again random.choice function just like what I have discussed earlier.

Add the following code to your editor to run the program.

import string
import random

print("+++Itsourcecode Password Generator+++")

mxchars = string.ascii_letters+string.digits+string.punctuation

numInput = int(input("Enter the Number of passwords to Generate: "))
length = int(input("Enter the Password Length: "))
print("+++++++List(s) Generated Password:++++++++ ")
for pwd in range(numInput):
pw = ''
for c in range(length):
pw += random.choice(mxchars)

If you run the code, you will be asked to enter the number of passwords to be generated.

Then, when you press “Enter” you will be asked again to input the password length.

Finally, you will be asked to press “Enter” for the last time.

and you will be given a list of generated passwords as shown below.

In this output, you will see that Python Generate Random String and Password with specific length.

This source code is useful in creating a strong password that has a mix of both lower and uppercase letters, numbers, and symbols.

Again you can include this code in your main method. The passwords should be random, especially when generating a new password every time the user asks for a new password.

Recommended Article from the Author

Final thought

In generating random passwords, there are so many ways and solutions to do this. But again it’s up to you how are going to challenge yourself to dig deeper.

But in our sample solution, we have made it to the point to make it clear, simple, and elegant.

What do you have in Mind?

I am excited to know your idea, what do you think about the article? Is there anything I missed?

I want you to know that I’m glad to hear from you. Please If you have any questions or suggestions, please feel free to leave a comment below.

Leave a Comment