DEV Community

Cover image for Create real strong password generator with python
Sahil Saif
Sahil Saif

Posted on

Create real strong password generator with python

How to make strong password generator with PYTHON ?

Let's see πŸ‘‡πŸ»

Step 1

β†’ First import build-in string module
β†’ store all characters ( uppercase or lower or digit or punctuation ) in their respective list
1

Step 2

β†’ Taking input for the numbers of characters in password and adding some conditions so that wrong input can be handle.
2

Step 3

β†’ import build-in random module and shuffle all 4 list
β†’ shuffle each list with random.shuffle(list_name)
β†’ we will create password with 30% of uppercase (characters number given by user) , 30% lowercase, 20% digits and 20% special character
3

Step 4

β†’ collecting characters from all 4 lists as the rule of 30%, 30% ,20%,20%
β†’ Now shuffle the final collected list for making the password pattern unguessed
β†’ Now join the final list elements with " ".join( )
4

FINAL code

import string
import random

s1=list(string.ascii_lowercase)
s2=list(string.ascii_uppercase)
s3=list(string.digits)
s4=list(string.punctuation)

characters_number=input("Enter the numbers of characters of your password: ")
while True:
    try:
        characters_number=int(characters_number)
        if characters_number < 6:
            print("you need at least 6 characters for a strong password !")
            characters_number=input("Enter the numbers of characters AGAIN: ")
        else:
            break
    except:
        print("X X X    please enter the number of characters ONLY !!!")
        characters_number=input("Enter the numbers of characters: ")

random.shuffle(s1)
random.shuffle(s2)
random.shuffle(s3)
random.shuffle(s4)
# 30% of the characters_numberπŸ‘‡πŸ»
end1=round(characters_number * (30/100)) 
# 20% of the characters_numberπŸ‘‡πŸ»
end2=round(characters_number * (20/100))    

s=[]
for i in range(end1):
    s.append(s1[i])
    s.append(s2[i])

for i in range(end2):
    s.append(s3[i])
    s.append(s4[i])

random.shuffle(s)
password="".join(s[0:])
print(f'your password is : {password}')
Enter fullscreen mode Exit fullscreen mode

check the output


Twitter

Discussion (0)