Random Password generator in Python

How I wrote a random secure password generator in Python in 30 min.

What I needed was a small script that I execute and generate a random password that is then copied to the clipboard and all I had to do was paste it Ctrl+V to use it.

Every time I run the script a new random password is generated and ready to be pasted.

This is made to work on Windows but if you need it to work on Linux, you can modify the os library or instead of os.system(“pause”) wait for user input with input(“Press Enter to continue…”).
Random passwords look like this: “a’vXu`2TzY,:8T4Gm,0
As I was writing this I’ve modified the script to remove the os import and make it more universal and work on Linux also.

Script name: call it whatever you like

Python 3 Code

# Code on GitHub: https://github.com/dragosion/GenPw2Clipboard
# Dragos Ion
#import os 
import string
import pyperclip # if missing install: pip install pyperclip  #https://pypi.org/project/pyperclip/
import secrets
import random

alphabet = string.ascii_letters + string.digits + string.punctuation 
while True:
    r1 = random.randint(14, 26)
    password = ''.join(secrets.choice(alphabet) for i in range(r1))
    if (sum(c.islower() for c in password) >=3
            and sum(c.isupper() for c in password) >=3
            and sum(c.isdigit() for c in password) >=3):


print("Password copied to clipboard. Pw length: " + str(r1))
input("Press Enter to continue...")

It generates a random password using the secrets library (more secure and more random).
Variable password lenght can be modified min max can be changed here: random.randint(14, 26)

Including letters, digits and punctuation, printable characters generally.

Looks useful? Star my repo on Github https://github.com/dragosion/GenPw2Clipboard


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.