Making a simple username and password generator in Python

In this tutorial, I am making a basic username and password generator in Python

Notes

I am going to be putting this all into a class called gen and you need to import the random module.

import random
class gen():
...

Generating the password

Generating passwords are pretty easier. All you have to do is randomly choose a character from a string with replacement a specified amount of times.

Lets first focus on generating the list. What I am doing is creating a function that combines 4 different strings together and returns it.

import random
class gen():
    def genlist():
        lower = "abcdefghijklmnopqrstuvwxyz"
        upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        num = '0123456789'
        symbols = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
        return lower + upper + num + symbols #simply returns a string of charaters
...

Now that we have our character set, we need to randomly pick a character from the string a certain number of times. We can do this by using the inbuilt function from the random module. We need to pass two parameters with this function, the list and the password length which can be done with k=pasLength.

...
    def genpassword(pasLength):
        list = gen.genlist() #gets the big list
        temp = random.choices(list, k=pasLength) #gets random charaters
        return ''.join(temp) #Changes list from list to string
...

Making the username

Making the username is a bit trickier then making a password with many different methods. What I did is combine two random words with a 3 digit number at the end.

To do this we need to get a word list. I got my word list from svnweb. I then just copied it the words into a text file called word.txt.

The first thing we have to do is make a list from this file. To do this, I created a function that iterates threw every line in the file and then strips all the words out and then appends it to a list that is returned.

   ...
    def word_list():
        words = open("words.txt", "r") #opens the wordlist
        word_list = [] #creates an empty list for it to add to
        for line in words: #repeats for as many lines in the text file
            stripped_line = line.strip() #strips all the lines
            line_list = stripped_line.split()  #strips the words
            word_list.append(line_list) #adds it to the variable word_lists
        words.close() #closes the document
        return word_list #returns a list full of words
...

Now that we have got the word list we need to randomly pick 2 words out of the list. We can randomly pick a word by using random.choice again then adding them onto a string by using the .join method.

...
    def genuser():
        word_list = gen.word_list() #gets the word list
        return "".join(random.choice(word_list)) + ''.join(random.choice(word_list)) + str(random.randrange(100, 999))
...

Outputting both the username and password

Now that we have the username and password generator, we now need to create a function the calls the genuser() and the genpassword() function and prints the output out nicely.

...
def genuser_password(amount, paslength):
        for i in range(0, amount): #loops for as many times as amount
            user = gen.genuser() #gets a random username
            password = gen.genpassword(paslength) #gets a random password with determined length
            print(f"Your username is: {user} | Your password is: {password}") #outputs it into terminal

You have now run your function by using gen.genuser_password(1, 8). For the full code snipet go here and if you want to see this with command line functionality go here.