In today’s digital world, security is one of the most important things. Since everything is going digital, we need to be highly concerned about security. To protect our accounts and data from anonymous people, we all use passwords daily. Password keeps our account safe by restricting other users from accessing the account. Although passwords are the most common types of security used in the digital world, they are not safe if we misuse them.
Many cracking tools are available for free that use some techniques like a brute force to crack a user password. This type of tool tries every sequence of characters and tries to crack the password. So to prevent this type of attack, we shall not choose a password that is of short length or can be easily guessed. While creating a password, we need to follow the below rules.
- The password length must be atleast 8 characters.
- The password should contain a mixture of both uppercase and lowercase characters.
- The password should contain a mixture of digits and letters.
- The password should contain at least one special character.
Creating a password generator using Python
In this tutorial, we will learn how to create a password generator using python. This password generator can be used to generate strong passwords based on the rules mentioned above. To follow this tutorial, it is assumed that you have a basic knowledge of python. It is recommended to execute all the code block yourself for better understanding. Then, you can use an IDE like the open-source VS code or PyCharm for writing your code.
Writing the code
Let us build our password generator. First, our password generator must ask the user to enter the length of the password. Next, we need to select random characters from the mixture of uppercase, lowercase, digits, and special characters. The random characters will be combined to form a string. This string will be our required password. So, we will display it to the user in the console.
To build our program, we first need to import the required libraries into our python program. To build the password generator, we will require two libraries. The first one is the string module. The string module has built-in attributes that store all the uppercase letters, lowercase letters, digits, and special characters. The second module that we will use is the random module. The random module has built-in functions that will provide randomness to our password generator. Our program will use the random module to select random characters for the password from a set of characters. So, let us import the required modules into our code.
import string import random
After importing the required libraries, we will convert the ascii_uppercase, ascii_lowercase, punctuations attributes of the string module into a list. Next, we will use the list comprehension of python to create a list of numbers from 0-9. See the below code for an illustration.
uppercase_letters = list(string.ascii_uppercase) lowercase_letters = list(string.ascii_lowercase) punctuations = list(string.punctuation) digits = [str(i) for i in range(10)]
After converting the string into a list, we need to create an empty list and extend it with the list of uppercase, lowercase, punctuation characters, and the digits that we have created. See the below code for an illustration.
characters =  characters.extend(lowercase_letters) characters.extend(uppercase_letters) characters.extend(punctuations)
Next, we use the input() function of python to prompt the user to enter the length of the password. After taking the length from the user, we will use the choice() function of the random module to choose random characters from the list of characters. Then, we use the join() method of the string to join the characters and convert them into a string. See the below code for illustration.
length = int(input('Enter the length of the password : ')) password = ''.join(random.choice(characters) for _ in range(length)) print(password)
In the above code, we use the choice() function of the random module to generate a random string of characters. This random string of characters will be our required password which can’t be cracked easily.
No, let us merge our codes to build our final password generator program. See the below code for an illustration. You can copy the code by clicking the copy button present in the top menu of the below code block.
import string import random uppercase_letters = list(string.ascii_uppercase) lowercase_letters = list(string.ascii_lowercase) punctuations = list(string.punctuation) characters =  characters.extend(lowercase_letters) characters.extend(uppercase_letters) characters.extend(punctuations) length = int(input('Enter the length of the password : ')) password = ''.join(random.choice(characters) for _ in range(length)) print(password)
The above code will prompt a user to enter the length of the password and then generate a strong password of the required length. On running the above code, we will get the output as shown in the below image.
In this tutorial, we have seen how we can use the string module and the random module of python to build a strong password generator. You may also want to see our step-by-step guide on generating QR codes in python.