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.