Password is a secret key which is required for login into the application. It secures your account from unauthorized access. There are many instances where you want a secure password for account creation on application.
Why do we need secure passwords?
Hackers can hack your account using brute force attack if you keep a less secure password. Hackers try permutation and combination of alphabets and numbers to match the password. Once matched they get access to the account.
A strong password is a complex combination of letters (uppercase and lowercase), numbers, and special characters. They are long, unusual, and unpredictable, allowing them to withstand attack attempts.
We will see python random password generator implementation
How to make a random password generator in python.
Python being a simple and multi purpose language. It has a lot of built in libraries / modules which make the development process simple. Python provides a built-in module called random that helps us to generate random data. We will create a custom function.
import random
import string
def generate_password(length=12):
password=”
characters = string.ascii_letters + string.digits + string.punctuation
for i in range(length):
password += random.choice(characters)
return password
random_password = generate_password()
print(“Random Password:”, random_password)
The output of above function looks like this
Random Password: ,dsF=5q1>”yB
In this script:
- We first import the random module for getting random values and the string module for accessing various string constants such as ASCII letters, digits, and punctuation.
- The generate_password function takes one optional argument called length. which is the length of the password. By default, it generates a 12-character long password.
- Inside the function, we add letters, digits, and punctuation to form the collection of characters from which we’ll randomly select for the password.
- We apply for loop with random.choice() function to randomly select characters from the collection and concatenate them to form the password
- Finally, we return the generated password.
But, If you want to create a python password generator that takes input parameters like number of alphabets, numbers and punctuation. You can follow the below function.
def custom_generate_password(letters=3,digits=2,punctuation=3):
elements_letters=”.join([random.choice(string.ascii_letters )
for i in range(letters)])
elements_digits=”.join([random.choice(string.digits)
for i in range(digits)])
elements_punctuation=”.join([random.choice(string.punctuation)
for i in range(punctuation)])
password = list(elements_letters+elements_digits+elements_punctuation)
random.shuffle(password)
password= “”.join(password)
return password
password=custom_generate_password(letters=3,digits=2,punctuation=4)
print(“Random Password:”, password)
Output:
Random Password: 3^EX&”z{4
In the above code, we first create three variables element_letters, elements_digits, element_punctuations. Which store alphabets, digits and punctuations with respect to length specified in input parameter. Concatenating the values of three variables and creating a list of all characters. We use the random.shuffle() function to shuffle the list and create a string out of the list which is our randomly generated password.
Conclusion
The strength of your passwords plays an important role in securing your digital assets. Next time you’re prompted to create a password, consider using python random password generator and secure one, keeping your accounts safe from cyber threats.
You can find the above code on GitHub.
Pingback: NumPy where function with multiple conditions - pythoncodelab.com