iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use Python’s secrets module—not random—to generate passwords or passphrases with security-sensitive randomness. The examples below let you choose the length, character set, or word count, while leaving site-specific password rules up to the destination.
Why use secrets instead of random?
Python’s secrets module is designed for cryptographically strong random values suitable for passwords and authentication secrets. The random module’s default generator is intended for modelling and simulation, not security. As the Python documentation puts it: “In particular, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not for security or cryptography.”
Generate a random character password
Build an alphabet from the characters you want to allow, then use secrets.choice to select each character independently. This example includes lowercase and uppercase letters, digits, and common punctuation:
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11import secrets
import string
alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
length = 20
password = ''.join(secrets.choice(alphabet) for _ in range(length))
print(password)
Change length to suit your use. Change alphabet if the destination rejects certain characters—for example, some forms exclude spaces or punctuation. The destination’s rules determine which characters are acceptable; the example’s length and character groups are implementation choices, not a universal account-password policy.
#1 Best Overall
- POWERFUL SECURITY KEY: The Security Key C NFC is the essential physical passkey for protecting your digital life from phishing attacks. It ensures only you can access your accounts.
- WORKS WITH 1000+ ACCOUNTS: Compatible with Google, Microsoft, and Apple. A single Security Key C NFC secures 100 of your favorite accounts, including email, password managers, and more.
- FAST & CONVENIENT LOGIN: Plug in your Security Key C NFC via USB-C and tap it, or tap it against your phone (NFC) to authenticate. No batteries, no internet connection, and no extra fees required.
- TRUSTED PASSKEY TECHNOLOGY: Uses the latest passkey standards (FIDO2/WebAuthn & FIDO U2F) but does not support One-Time Passwords. For complex needs, check out the YubiKey 5 Series.
- BUILT TO LAST: Made from tough, waterproof, and crush-resistant materials. Manufactured in Sweden and programmed in the USA with the highest security standards.
Require particular character groups when needed
Some destinations require at least one lowercase letter, uppercase letter, or digit. You can generate a candidate and check those conditions, repeating until one passes:
import secrets
import string
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
digits = string.digits
alphabet = lowercase + uppercase + digits
length = 16
while True:
password = ''.join(secrets.choice(alphabet) for _ in range(length))
if (any(char in lowercase for char in password)
and any(char in uppercase for char in password)
and any(char in digits for char in password)):
break
print(password)
Only add checks that match the destination’s actual requirements. If the required groups cannot fit within the chosen length, the loop cannot succeed; use a length large enough to satisfy every required group. For a fixed length, this retry approach samples valid strings by generating from the full alphabet and rejecting strings that fail the checks.
Rank #2
- Requires 3 "AAA" batteries (included)
- Unit auto-locks for 30 minutes after 5 consecutive incorrect PINs
Generate a word-based passphrase
A passphrase selects words independently from a list. This small example demonstrates the method; replace the sample list with a larger, suitably curated list for practical use:
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →import secrets
words = ["copper", "meadow", "orbit", "lantern", "willow", "harbor"]
word_count = 5
passphrase = '-'.join(secrets.choice(words) for _ in range(word_count))
print(passphrase)
The separator and word count are adjustable. Make sure the destination accepts the chosen characters, including any separator. A short demonstration list is not a strength recommendation: the number of possible selections depends on the size of the word list and the number of independently selected words.
Rank #3
Choose between a character password and a passphrase
| Consideration | Character password | Passphrase |
|---|---|---|
| Selection | Choose characters from an alphabet with secrets.choice. |
Choose words from a word list with secrets.choice. |
| Destination compatibility | Check the destination’s accepted characters and any required groups. | Check whether words, separators, and the resulting length are accepted. |
| Length control | Set the number of independently selected characters. | Set the number of independently selected words. |
| Usability | Often compact, but may be harder to remember and type. | Words may be easier to read or remember, though a longer phrase may be needed. |
Neither format is automatically right for every use. A password for a sign-in form must comply with that service’s constraints; a machine token may have different format requirements and may not need to be memorable.
Do not confuse generating a password with storing one
A local script that generates and displays a new password is different from an application that accepts and stores users’ account passwords. If your application handles account passwords, do not store them in plaintext or in a recoverable encrypted form. Python’s documentation recommends salted, cryptographically strong one-way hashing for password storage; consult its security guidance when designing that separate part of an application.
Rank #4
- FIDO-ONLY FUNCTIONALITY: Supports FIDO2 (passkeys) and FIDO U2F protocols for passwordless and second-factor authentication. Does not support OTP, TOTP, Smart Card (PIV), or other advanced features - upgrade to YubiKey 5 Series for extended functionality
- SECURE AND CONVENIENT: Passwordless MFA login with the YubiKey Bio authenticator and biometric information using a fingerprint, with a PIN as a fallback. Simply plug in via USB and use your fingerprint to authenticate
- DEVICE & OS COMPATIBILITY: Compatible with Windows, macOS, ChromeOS, and Linux. Works seamlessly with supported services like Google and Microsoft accounts, and major password managers. See the full compatibility list at "Works With YubiKey"
- DURABLE & RELIABLE: Resistant to tampering, water, and crushing. No batteries or network connectivity required, offering dependable authentication without any downtime. Securely manufactured in USA & Sweden
- Yubico Authenticator App - Fingerprint enrollment, passkey management and PIN configuration available via the app app - Upgrade to YubiKey 5 Series to generate one-time-passwords (OTP) via Yubico Authenticator and for advanced compatibility (OATH, PIV)
What the 32-byte figure does—and does not—mean
The Python documentation says that, as of 2015, 32 bytes (256 bits) of randomness was believed sufficient for the typical use case expected for the secrets module. That statement concerns typical token use and is not a current universal password-length rule. The documentation also warns that the default entropy for tokens may change, including in maintenance releases. For a password generator, choose a character length or word count appropriate to the destination rather than treating 32 bytes as a mandated account-password size.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

