Hello, Pythonists! Welcome to another exciting Python tutorial. In this Python tutorial, you will know how to create a random password generator in python.
Sometimes we need to give some strong passwords, so we will think about how to give strong passwords and also sometimes we need a random strong password when you signup in some rarely used websites.
So we need some strong password for signup or some other work. You know you don’t need to search online to get some strong password, actually, you can build your own random password generator in python.
You know, believe or not some websites are built on the same concepts, I will give the links of it, please visit before you start coding, so that you can get a clear picture of what you are going to learn.
Random password generator Example webisite links:
Yes, you can develop it for your work or something else. If you don’t know how to do it, don’t worry I will teach you how to build your own random password generator in python programming language.
Building a Random password generator in Python
To create a strong random password we are going to use python random module, which helps to get a random character each time.
First, we need to take care of strong password conditions. I will give all of them below.
- Password should be atleast 8 charecters.
- Password have at least 1 lowercase letter
- Passowrd have at least 1 Uppercase letter
- Password have at least 1 number
- Passowd have at least Special charecter
you know these are the best conditions to give a strong password when you create a new account on websites.
Read more Articles on Python
we are going to build a strong random password generator in python with the above conditions. so let’s get started..!
Here I give the entire code for generation random passwords using python, I surely explain each line of it the below.
import random alphabets =['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'] small = ['a','b','c','d','e','z','o','t','h','l','x','w'] numbers = [1,2,3,4,5,6,7,8,9,0] chars = ['!','@','#','$','*','|','%'] ran_1 = random.choice(small) + str(random.choice(numbers)) + random.choice(chars) + random.choice(alphabets) ran_2 = random.choice(small) + str(random.choice(numbers)) + random.choice(chars) + random.choice(alphabets) random_password = ran_1 + ran_2 print("your random password is:", random_password)
I imported the python random module to get random characters in each set.
I created 4 lists that are holding uppercase letters, lowercase letters, special characters, and numbers.
I used random.choice() to choose one random character in each list. so that I can get one random character from alphabets, chars, small and numbers.
I combined all of them in a string format, now I got a new random word with 4 character length. but we need at least 8 characters.
so I wrote the same line in the below and concatenate them and assign them to random_passoword. So that I can get 8 characters strong random passwords.
Even you can use python function to get the same output, but I wrote it in a simple way.
If you love this tutorial, Subscribe to our newsletter to get my new interesting articles on python and other libraries. If you have any doubts please comment below. Thank you.!