Last active
March 10, 2024 18:39
-
-
Save EgorBron/2c01f0327c254cb01085b59b46ccce2c to your computer and use it in GitHub Desktop.
Generate random password using just one line of Python code!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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