Sep. 24, 2012

Python Password Generator

Password Generator

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.
Concatenation of the ascii (upper and lowercase) letters

The string '0123456789'. 	

String of ASCII characters which are considered punctuation characters in the C 

print string.ascii_letters

print string.digits

print string.punctuation



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 Snippets page

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, only recommend products or services that we try personally and believe will add value to our readers.