In this tutorial, we will create a random password generator using simple code. This is very important to keep your password strong 💪 to avoid access to your online website accounts by just guessing the password.
Here is the code to generate a random password and just below the code you can find an explanation of each line
import random
lower = "abcdefghijklmnopqrstuvwxyz"
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
symbols = "[]{}()!@#$%^&_-*"
all = lower + upper + numbers + symbols
length = 16
password = "".join(random.sample(all, length))
print("Strong Password: ", password)
Strong Password: 8T{^sXD$ikK3yw&n
import random
We import the random module to generate a random string from a given sample.
all
This is the sample string from which we will generate a password this is the combination of lower
, upper
, numbers
, symbols
strings.
length
It is just a number that indicates the length of a password. (we can change it according to requirement)
random.sample(all, length)
This function accepts two parameters sample string and length. It will return the random string from a given sample and its length will be equal to provided length parameter.
print("Strong Password: ", password)
Here we are printing the generated password.
Some tips to extend this project:
- ✔Change the sample string by taking input from the user.
- ✔Take a value of length from the user.
- ✔Copy this password on the clipboard automatically so that the user will directly paste it where he/she want to use it.
Now, it's your turn to try it out, modify the code and come with some awesome password generators.
🎉Happy Coding!