Python: Merge Multiple PDFs into One
Merge several PDF files into one with Python in a few lines — keep the page order you choose.
Generate a strong random password with Python using the secrets module — never use random() for passwords.
12 lines · Updated Nov 26, 2026
import secrets
import string
def generate_password(length=16, use_symbols=True):
letters = string.ascii_letters + string.digits
if use_symbols:
letters += "!@#$%^&*"
return "".join(secrets.choice(letters) for _ in range(length))
if __name__ == "__main__":
print(generate_password(16)) # e.g. k7#Qm2$xP9!vB4z
print(generate_password(12, False)) # letters and digits only
Weak and reused passwords are still the most common way accounts get taken over. This snippet generates strong random passwords in Python using the secrets module — the standard library's cryptographically secure random generator — so you can create passwords for new users, API keys, database accounts or test data safely.
secrets and not random?Python's random module is designed for simulations and games; its output can be predicted if an attacker sees enough values. secrets uses the operating system's secure source of randomness and is the right tool for anything security-related.
secrets.choice() up to the requested length.O, 0, l, 1) for passwords people must type by hand.secrets.token_urlsafe(32) is a one-liner.Should I store generated passwords? Never store user passwords in plain text — hash them (for example with bcrypt or Argon2). Use a password manager for your own credentials.
Related: OWASP Top 10 explained · 10 Python automation projects
Save as a script or import the function into your project. The secrets module is in the standard library — no install needed. Never use random for passwords or tokens.
Share this snippet
Merge several PDF files into one with Python in a few lines — keep the page order you choose.
Read the text out of any PDF with Python — the first step for search, summaries and automation.
Send yourself a Telegram message from Python — free alerts for scripts, servers and price trackers.