Skip to content

Instantly share code, notes, and snippets.

@EgorBron
Last active March 10, 2024 18:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgorBron/2c01f0327c254cb01085b59b46ccce2c to your computer and use it in GitHub Desktop.
Save EgorBron/2c01f0327c254cb01085b59b46ccce2c to your computer and use it in GitHub Desktop.
Generate random password using just one line of Python code!
# pyperclip is recommended (pip install pyperclip)
# you can replace `__import__("pyperclip").copy(...)` with just `print(...)`
# one line verson
__import__("pyperclip").copy("".join(__import__('random').choices(__import__('string').printable[:-6], k=24))) # ; print("Generated"); __import__("time").sleep(3)
# "prettier" version
__import__("pyperclip").\
copy(
"".join(
__import__('random').\
choices(
__import__('string').\
printable[:-6],
k=24
)
)
) # ; print("Generated"); __import__("time").sleep(3)
# explaination
import pyperclip
import string
import random
# use list of all printable characters except whitespaces (last 6)
symbols: str = string.printable[:-6]
# select random element from list 24 times
# `random.choices` is just a shorthand for loop in range(24) + `random.choice`
selected: list[str] = random.choices(symbols, k=24)
# join the selection with empty string
password: str = "".join(selected)
# now copy
pyperclip.copy(password)
# optional: add delay after
# import time
# print("Generated")
# time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment