Python generates a random number password of the specified length

  • 2020-04-02 13:21:47
  • OfStack


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# The import random and string The module 
import random, string
def GenPassword(length):
    # Number of random Numbers 
    numOfNum = random.randint(1,length-1)
    numOfLetter = length - numOfNum
    # The selected numOfNum A digital 
    slcNum = [random.choice(string.digits) for i in range(numOfNum)]
    # The selected numOfLetter A letter 
    slcLetter = [random.choice(string.ascii_letters) for i in range(numOfLetter)]
    # Disrupt this combination 
    slcChar = slcNum + slcLetter
    random.shuffle(slcChar)
    # Generate the password 
    genPwd = ''.join([i for i in slcChar])
    return genPwd
if __name__ == '__main__':
    print GenPassword(6)


Related articles: