Python @ DjangoSpin

PyPro #28 Generating a Random Password

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Generating a Random Password in Python: Write a Python program which generates a random password. The password may consist of alphabets, numbers & special symbols. Its length should be between 8 and 12. The program will take no inputs, it will output a random password when executed. Hint: You can use the randint() of random module to pick a random length for your password, and choice() function of random module to choose a random characters out of a predefined universal character set.

Generating a Random Password in Python

import random

unifiedCharacterList = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()_+"

def generateRandomPassword():
    length = random.randint(8, 12)                              # will determine the length of the password

    password = ""                                               # initialize an empty container which will be populated below

    for index in range(length):                                 # for each position of password
        randomCharacter = random.choice(unifiedCharacterList)   # pick a random character
        password = password + randomCharacter                   # append the randomly chosen character to the password variable

    return password

passwordOne = generateRandomPassword()

print(passwordOne)
Try it here.

See also:


Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon