Method 1 , Upper and lower case letters + number ：
import random import string ran_str =
''.join(random.sample(string.ascii_letters + string.digits, 8)) print ran_str
Method 2 , Upper and lower case letters + number + Special characters ：
application python random Standard library to do a random password generation procedures , Any number of characters can be generated randomly .( be based on python2.7, If it is python3 It needs to be modified )
#-*-coding:utf-8 -*- import random import string import sys # Store upper and lower case letters and numbers , Special character list
STR = [chr(i) for i in range(65,91)] #65-91 Corresponding character A-Z str = [chr(i) for i in
range(97,123)] #a-z number = [chr(i) for i in range(48,58)] #0-9 # Getting a list of special strings is a bit different
initspecial = string.punctuation # This function gets all the special characters , The result is a string special = [] # Define an empty list
# Making a list of special symbols for i in initspecial: special.append(i) total = STR + str + number +
special #print total choices = ['6','8','10','16'] def
Randompassword(your_choice): if your_choice in choices: passwordli =
random.sample(total,int(your_choice))
##sample The function takes values from several lists and returns a new list , Here is a list that needs to be converted to a string to be displayed passwordst =
''.join(passwordli) # Now you get the converted string ‘’ Is the separator , It can be used for ; : . wait print
"\033[32m Generated \033[0m" + your_choice + "\033[32m The password is :\033[0m\n" + passwordst
else: print "\033[31m Please enter the specified number of digits (6,8,10,16) \033[0m" if __name__ == '__main__':
while True: choice =
raw_input("\033[33m Please enter the number of digits you want to get the random password ：(6,8,10,16), Or input q sign out \033[0m\n") if choice !=
'q': # input q Then exit the cycle Randompassword(choice) # Executive function else: break
Method 3 , letter + number ：
#!/usr/bin/env python # -*- coding=utf-8 -*- import random, string
# Import random and string modular def GenPassword(length): # Number of random numbers numOfNum =
random.randint(1,length-1) numOfLetter = length - numOfNum # Select numOfNum A number
slcNum = [random.choice(string.digits) for i in range(numOfNum)]
# Select numOfLetter Two letters slcLetter = [random.choice(string.ascii_letters) for i in
range(numOfLetter)] # Disorganize the combination slcChar = slcNum + slcLetter random.shuffle(slcChar)
# Generate random password getPwd = ''.join([i for i in slcChar]) return getPwd if __name__ ==