Password Generator Using Python 3 Code

      No Comments on Password Generator Using Python 3 Code

We generate a random password(s) that can be used in database projects or to automate login password creation. In this example we are using Python 3.8 but I believe anything above Python 3 should work. My challenge was that random generators tend to use all of the characters available including troublesome characters like “%”, or “&”.

To solve this problem I added an exclusion, as opposed to include, to the process so it can still generate randoms, scramble them and avoid causing code errors. The passwords are stored in a Python List so they are easy to work with and can be retrieved as strings quickly.

Random Password Generator Criteria

  1. Generate a random password using Python’s standard string
  2. Include the ability to create multiple passwords
  3. Include the option to change the length of the password
  4. Add the ability to exclude certain characters like & or %
  5. Return the passwords in a Python List for ease of use

Python string and random Libraries

We are using the Python string and random libraries so we don’t need to install or import any additional modules. The string library will provide all the characters and numbers we need.

import string
import random

allchars = string.ascii_letters
allnums = string.digits
allpunc = string.punctuation

Password Length and Exclude Variables

The first step is to assign variables so we can define exactly how many characters, numbers and punctuation is required. This also defines the final length of the password.

Lastly we create a List to store the characters that we do NOT want in the final password. In my example I am removing the &, % and tilde (`) as well as the single and double quotes. Since we are storing the final password in a List I did not want the quotes to interfere with our results.

The “numpwds” is simply the number of passwords we want to generate.

numchars = 8  ## number of characters, upper and lower
numdigits = 5 ## number of digits
numpunc = 4 ## number of punctuation characters
exclude = ["%","&","\"","'","`"] ## exclude character(s) as a list

numpwds = 10 # number of passwords to generate

Password Characters and Numbers

The next step is to assign characters to variables so we can run the exclusion and scramble them later. The string library offers the ability to assign these 3 different types of characters. We don’t want a pattern like lowercase + uppercase + number to make the password more predictable.

allchars = string.ascii_letters
allnums = string.digits
allpunc = string.punctuation

Exclude Characters

In addition we want to run these variables through a “for loop” to remove the excluded characters if needed. The Python string library has an option to “replace” a character in the string with another. We simply replace the excluded characters with nothing.

for ex in exclude:   ## Python List
    allchars = allchars.replace(ex, '')
    allnums = allnums.replace(ex, '')
    allpunc = allpunc.replace(ex, '')

Use Shuffle to Randomize

We first create an empty List to append to later and we can now use a “while” loop to generate each password. You will notice that the random (db tales com) command is used to not only randomize the characters but also to limit the number base on our previous variables.

Also, random has another method called shuffle that we use to re-arrange the characters after they have been concatenated together. Finally we “join” the characters into one string and append the final password to our List.

paswdlist=[]

i = 1
while i <= numpwds:
    combine = random.choices(allchars, k=numchars) + random.choices(allnums, k=numdigits) + random.choices(allpunc, k=numpunc)
    random.shuffle(combine)  # shuffle the characters
    converttostring = ''.join(combine)
    paswdlist.append(converttostring)
    i += 1

With the passwords stored in a list we can use a for loop to grab them one at a time.

Note: I was able to create 100,000 passwords in a few seconds using VS Code. I tried a million but it seems to stall.

Random Password Generator Full Example Code

import string
import random

numchars = 8    ## number of characters, upper and lower
numdigits = 5   ## number of digits
numpunc = 4     ## number of puctuation characters
exclude = ["%","&","\"","'","`"]   ## exclude character(s) as a list

numpwds = 1   ## number of passwords to generate

allchars = string.ascii_letters
allnums = string.digits
allpunc = string.punctuation

for ex in exclude:
    allchars = allchars.replace(ex, '')
    allnums = allnums.replace(ex, '')
    allpunc = allpunc.replace(ex, '')

paswdlist=[]

i = 1
while i <= numpwds:
    combine = random.choices(allchars, k=numchars) + random.choices(allnums, k=numdigits) + random.choices(allpunc, k=numpunc)
    random.shuffle(combine)  # shuffle the characters
    converttostring = ''.join(combine)
    paswdlist.append(converttostring)
    i += 1

print(paswdlist)  ## Python List of passwords

for x in paswdlist:  ## individual passwords
    print(x)