应用python random标准库做一个随机生成密码的程序,可以随机生成任意多个字符。(基于python2.7,如果是python3需要修改下)

案例:

#-*-coding:utf-8 -*-
#author:wangxing import random
import string
import sys #存储大小写字母和数字,特殊字符列表
STR = [chr(i) for i in range(65,91)] #65-91对应字符A-Z
str = [chr(i) for i in range(97,123)] #a-z
number = [chr(i) for i in range(48,58)] #0-9 #特殊字符串列表获取有点不同
initspecial = string.punctuation #这个函数获取到全部特殊字符,结果为字符串形式
special = [] #定义一个空列表 #制作特殊符号列表
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函数作用是取几个列表里的值并返回一个新列表,此处得到的是列表需要转换为字符串显示出来
passwordst = ''.join(passwordli) #现在得到的是转换后的字符串 ‘’是分隔符,里面可以为; : . 等等
print "\033[32m生成的\033[0m" + your_choice + "\033[32m位数密码为:\033[0m\n" + passwordst else:
print "\033[31m请输入指定位数(6,8,10,16) \033[0m" if __name__ == '__main__':
while True:
choice = raw_input("\033[33m请输入你要得到随机密码的位数:(6,8,10,16)\033[0m\n")
if choice != 'q': #输入q则退出循环
Randompassword(choice) #执行函数
else:
break

python random使用生成随机字符串的更多相关文章

  1. [ Python - 5 ] 通过random模块生成随机字符串

    import random checkcode = '' for i in range(4): if i == random.randint(0,3): current = chr(random.ra ...

  2. python 生成随机字符串

    1.生成随机字符串 #数字+字母+符号 def getRandChar(n): l = [] #sample = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^ ...

  3. .net生成随机字符串

    生成随机字符串的工具类: /// <summary> /// 随机字符串工具类 /// </summary> public class RandomTools { /// &l ...

  4. JS生成随机字符串的多种方法

    这篇文章主要介绍了JS生成随机字符串的方法,需要的朋友可以参考下 下面的一段代码,整理电脑时,记录备查. <script language="javascript"> ...

  5. PHP中生成随机字符串,数字+大小写字母随机组合

    简单的生成随机字符串: /* * 生成随机字符串 * * $length 字符串长度 */ function random_str($length) { // 密码字符集,可任意添加你需要的字符 $c ...

  6. iOS开发:Swift/Objective-C高效生成随机字符串

    原文连接 Objective-C版 // 随机生成字符串(由大小写字母.数字组成) + (NSString *)random: (int)len { char ch[len]; for (int in ...

  7. PHP 生成随机字符串与唯一字符串

    说明:生成随机字符串用到的方法有 mt_rand() 生成唯一字符串用到的方法有 md5(),uniqid(),microtime() 代码: <?php /* * 生成随机字符串 * @par ...

  8. PHP生成随机字符串包括大小写字母

    PHP生成随机字符串包括大小写字母,这里介绍两种方法: 第一种:利用字符串函数操作 <?php /** *@blog <www.phpddt.com> */ function cre ...

  9. 生成随机字符串(UUID方法)

    这是另一种用UUID生成随机字符串的方法. public class RandomGenerator{ private int length; public void setLength(int le ...

随机推荐

  1. 个案排秩 Rank (linear algebra) 秩 (线性代数)

    非叫“秩”不可,有秩才有解_王治祥_新浪博客http://blog.sina.com.cn/s/blog_8e7bc4f801012c23.html 我在一个大学当督导的时候,一次我听一位老师给学生讲 ...

  2. <2014 03 18> Term BreakPoint

  3. Java 语言基础(一)

    大多数编程语言都包括以下基本内容: 关键字 标识符 注释 常量和变量 运算符 语句 函数 数组 学习语言最重要的两点: 该语言基础的表现形式是什么 这些东西什么时候使用 关键字 在程序语言中有特殊含义 ...

  4. paper reading:gaze tracking

    https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Krafka_Eye_Tracking_for_CVPR_2016_ ...

  5. Linux上free命令的输出及其他

    一.明确概念 A buffer is something that has yet to be "written" to disk.  A cache is something t ...

  6. java内存相关

    (类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象是具体的,占用存储空间.) 1.java是如何管理内存的 java的内存管理就是对象的分配和释放问题.(其中包括两部分) 分配:内 ...

  7. 对 tensorflow 中 tf.nn.embedding_lookup 函数的解释

    http://stackoverflow.com/questions/34870614/what-does-tf-nn-embedding-lookup-function-do embedding_l ...

  8. HDU1081:To The Max(最大子矩阵,线性DP)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...

  9. win10下的linux一些问题

    1.文件位置在: C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\Loc ...

  10. sql join on 与where

    转载:http://www.cnblogs.com/Jessy/p/3525419.html left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : ...