Python can be a really useful tool to do things like creating random strings. There could be dozens of different reasons why you might want to create a random string of characters and numbers, but one of the more common ones is to use this string as a password.

Python Code Snippets offers this really useful snippet for generating random strings as a password generator that can easily be used in any of your projects that run on Python. In the snippet, the password generator creates a random string with a min of 8 characters and a max of 12, that will include letters, numbers, and punctuation. When the string has been generated, it's printed. Then your users (or you) are free to use it for any of their password needs.

We've adapted the snippet to our own liking and included it below. Check it out and feel free to use it, modify it, or completely customize it.

import string
from random import *
min_char = 8
max_char = 12
allchar = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(allchar) for x in range(randint(min_char, max_char)))
print "This is your password : ",password
To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

  • Manoj Kumar

    Global allchar i think need to be specified