DEV Community

Qu35t
Qu35t

Posted on

Random Password Generator

image

We all use passwords on a regular basis; however, in order to keep your account safe and prevent it from being stolen, you must create a password that is difficult to guess.
Password generator is a program that produces a password with a mix of upper and lowercase letters, numbers, and symbols that is strong enough to provide outstanding protection.

What is a Password:

A password, also known as a passcode, is a secret that is memorized and normally consists of a string of characters that is used to verify a user's identity during the authentication process. If you're interested in learning more, refer toPassword

Applicable Modules:-

String Module:

The string module contains a number of useful constants, classes and a number of functions to process the standard python string.
1. string.ascii_letters: Concatenation of the ascii (upper and lowercase) letters
2. string.ascii_lowercase: All lower case letters
3. string.ascii_uppercase: All Upper case letters
4. string.digits: The string ‘0123456789’.
5. string.punctuation: String of ASCII characters which are considered punctuation characters in the C locale.

Random Module.

Random module is used to perform the random generations. We are making use of random.sample module here. If you will observe in the output all characters will be unique. random.sample() never repeats characters. If you don’t want to repeat characters or digits in the random string, then use random.sample() but it is less secure because it will reduce the probability of combinations because we are not allowing repetitive letters and digits.

Now that you are familiar with password use cases and have acquired basic knowledge of random and string module, we can move forward to the coding section.

Code
In order to access the Python library, we need to import the package in our Python script.

import random
import string
Enter fullscreen mode Exit fullscreen mode

Let's greet the user !

print('Hi, Welcome to Password generator')
Enter fullscreen mode Exit fullscreen mode

you can ask the user for the password length

#length of Password

length = int(input('\nEnter the length of password: '))

Enter fullscreen mode Exit fullscreen mode

or

Choose the length of the password you want to generate.

#length of Password

length = 8

Enter fullscreen mode Exit fullscreen mode

The data must now be defined. For this, we'll employ the string module.


#define data
lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation 

Enter fullscreen mode Exit fullscreen mode

Store the data by combining the stored lowercase and uppercase letters, as well as numbers and symbols.

#combine the data

all = lower + upper + num + symbols

Enter fullscreen mode Exit fullscreen mode

From the data we have, use the random module to generate a password. Passing in the combined data, as well as the password length, and finally joining them.

#use random

temp = random.sample(all,length)

#create the Password
password = "".join(temp)
Enter fullscreen mode Exit fullscreen mode

We can further minimize the amount of lines of code by removing the data storage now that you have a clear knowledge of the script. Let's take a closer look.

all = string.ascii_letters + string.digits + string.punctuation
pass = "".join(random.sample(all,length))
Enter fullscreen mode Exit fullscreen mode

Finally, print the password!

print(password)

Enter fullscreen mode Exit fullscreen mode

a few examples of output:

#SAMPLE O/P: (length = 8)
[Y+4TEsA
c.@2XV:;
*\vbEQ[W
I.;F)L!~
X]60Js_3
G{CK%fm#
Enter fullscreen mode Exit fullscreen mode

Source 1

#import the necessary modules

import random
import string

print('Hi, Welcome to Password generator')

#length of Password
length = 8

#define data
lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation 

#combine the data

all = lower + upper + num + symbols

#use random

temp = random.sample(all,length)

#create the Password
password = "".join(temp)

#print password

print(password)
Enter fullscreen mode Exit fullscreen mode

Source 2

#import the necessary modules

import random
import string

print('Hi, Welcome to Password generator')

#length of Password
length = 8

all = string.ascii_letters + string.digits + string.punctuation
password = "".join(random.sample(all,length))

print(password)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)