Random Password Generator
Published: 2021-07-25 01:57:02

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8m6fev1qhcxmo76eg8ly.png)

 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 to[Password](https://en.wikipedia.org/wiki/Password)

####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.

```python
import random
import string
```
Let's greet the user !

 ```python
print('Hi, Welcome to Password generator')
```
you can ask the user for the password length 

```python
#length of Password

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

```
or

Choose the length of the password you want to generate.


```python
#length of Password

length = 8

```

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

```python

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

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

```python
#combine the data

all = lower + upper + num + symbols

```
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.

```python
#use random

temp = random.sample(all,length)

#create the Password
password = "".join(temp)
```
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.
```python
all = string.ascii_letters + string.digits + string.punctuation
pass = "".join(random.sample(all,length))
```

Finally, print the password!

```python
print(password)

```

a few examples of output:

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

Source 1
```python
#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)
```

Source 2 

```python 
#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)

```