Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/passphrases/python$ _

Generate Passphrases in Python

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Python

What: Generate cryptographically secure passphrases in Python using the <code>secrets</code> module introduced in Python 3.6. The Diceware method creates memorable yet highly secure passphrases.

Try it: Use our interactive Passphrases generator or integrate this code into your Python application.

Generate cryptographically secure passphrases in Python using the secrets module introduced in Python 3.6. The Diceware method creates memorable yet highly secure passphrases. Looking for other languages? Check our code examples in PHP , JavaScript , Java , C# , C++ , Ruby and Go or use our interactive web generator.

Python Code Example

import secrets

def generate_passphrase(word_count=6, separator='-'):
    """Generate a Diceware-style passphrase using EFF wordlist."""
    # EFF's improved wordlist (7776 words)
    wordlist = ["ability", "able", "about", "above", "accept", # ... 7771 more
    ]

    # Use secrets.choice() for cryptographic randomness
    words = [secrets.choice(wordlist) for _ in range(word_count)]

    return separator.join(words)

# Generate 6-word passphrase (recommended)
passphrase = generate_passphrase(6)
print(passphrase)
# Output: "galaxy-thunder-phoenix-cascade-velocity-quantum"

# Generate 8-word passphrase for higher security
strong_passphrase = generate_passphrase(8)
print(strong_passphrase)
# Output: "cosmic-mountain-radiant-freedom-nebula-infinite-crystal-harmony"

# Calculate entropy
import math
entropy = word_count * math.log2(len(wordlist))
print(f"Entropy: {entropy:.1f} bits")  # 77.5 bits for 6 words

[EXPLANATION]

Python's secrets module is specifically designed for generating cryptographically strong random values suitable for security applications. Unlike random.choice(), secrets.choice() uses the operating system's CSPRNG. The Diceware method with 6 words provides approximately 77.5 bits of entropy, which is stronger than most complex passwords.

Expected Output

galaxy-thunder-phoenix-cascade-velocity-quantum
cosmic-mountain-radiant-freedom-nebula-infinite
starlight-harmony-eclipse-brilliant-zenith-jupiter

Common Use Cases

  • CLI password manager tools (like pass)
  • Django/Flask user authentication systems
  • Python-based security tools
  • Backup encryption passphrases
  • System administration automation scripts

Important Notes

  • Always use secrets module, not random
  • Load wordlist from file for production use
  • EFF wordlist available at: https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt
  • 6 words = strong, 7 words = very strong, 8 words = extremely strong
  • Consider xkcdpass package for production

Try Our Interactive Generator

Don't want to write code? Use our free web-based Passphrases generator with instant results.

TRY PASSPHRASES GENERATOR →