python生成指定长度的随机数密码

yipeiwu_com6年前Python基础

复制代码 代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#导入random和string模块
import random, string

def GenPassword(length):
    #随机出数字的个数
    numOfNum = random.randint(1,length-1)
    numOfLetter = length - numOfNum
    #选中numOfNum个数字
    slcNum = [random.choice(string.digits) for i in range(numOfNum)]
    #选中numOfLetter个字母
    slcLetter = [random.choice(string.ascii_letters) for i in range(numOfLetter)]
    #打乱这个组合
    slcChar = slcNum + slcLetter
    random.shuffle(slcChar)
    #生成密码
    genPwd = ''.join([i for i in slcChar])
    return genPwd

if __name__ == '__main__':
    print GenPassword(6)

相关文章

python使用calendar输出指定年份全年日历的方法

本文实例讲述了python使用calendar输出指定年份全年日历的方法。分享给大家供大家参考。具体实现方法如下: import calendar print "Show a giv...

详解Python核心对象类型字符串

Python的字符串的特点 Python与C语言,Java语言都不一样,没有单个字符,只有一个有一个字符的字符串。 字符串对象不可修改,属于不可变类型 字符串和列表,元组都...

python+matplotlib绘制简单的海豚(顶点和节点的操作)

python+matplotlib绘制简单的海豚(顶点和节点的操作)

海豚 本文例子主要展示了如何使用补丁、路径和转换类绘制和操作给定的顶点和节点的形状。 测试可用。 import matplotlib.cm as cm import matplotl...

在dataframe两列日期相减并且得到具体的月数实例

如下所示: df = pd.DataFrame(np.random.randn(6,4), index=pd.date_range('20130101', periods=6), c...

Python中多个数组行合并及列合并的方法总结

采用numpy快速将两个矩阵或数组合并成一个数组: import numpy as np 数组 a = [[1,2,3],[4,5,6]] b = [[1,1,1],[2,2,...