Python Password Generator - PythonForBeginners.com

You can use Pythons string and random modules to generate passwords.

The string module contains a number of useful constants and classes.

Some of them we are going to use in this script.

string.ascii_letters
Concatenation of the ascii (upper and lowercase) letters

string.digits
The string ‘0123456789’.

string.punctuation
String of ASCII characters which are considered punctuation characters in the C
locale.

print string.ascii_letters

print string.digits

print string.punctuation

Output

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
!”#$%&'()*+,-./:;<=>?@[]^_`{|}~

Password Generator Script

So, to put this all together, we can use a pretty nice password generator script.

import string
from random import *
characters = string.ascii_letters + string.punctuation  + string.digits
password =  "".join(choice(characters) for x in range(randint(8, 16)))
print password

More Reading

For more scripts, please see the Code Examples page.

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.