python-生成随机字符
需求:
随机生成6位的大写字母:
方法一:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
li = []
for i in range(6):
temp = random.randrange(65,91)
c = chr(temp)
li.append(c)
result = "".join(li)
print(result)
结果:会随机生成6个字母
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOMAAABSCAIAAAApevTLAAAFOklEQVR4nO2bO5biOhCGtSwvYXbBJGgLZPQKrIzkhqROWwdCshuQOxifazvs4034Brbl0sMPBDRdzP+dCbCQRUnzWSoJWtR1fT3udsdrXdd1XZ/P53qg6t6oKlNyPe7S83jZVzunm/RcA/BMRFUZIauyPJ3SdLNJT2VZVVXZvVGW1cD1uPsdYpOeKgCeiWWq4ZRuOgWd8utxl57KyqY8pTAVPBtRAsABmAp4IP4DgAMwFfAApgIewFTAA5gKeCD+AYAD4uvrqwXgxwNTAQ9gKuABTAU8+B5TG5UcVPH8z/luuPSLS5xzRJvaqGQvxEf3L1HNfO1CHbw6uRRjC0J8iOTCbjCf3a9CXfS9MXbt8IizLS6JiYrEo+U+ztRGJXs5hFaog5D5cgSBgcilyMYe6my5nZ/GE/vVmZQ9zAAGcTZqOwZJvdLqEmOqr6ZWy4+jlqPcA/ZIuZc8eGC/vJnpkQPCIc6peBql8hhT6YQaolHJPrCUBB7ZdzD1cf3yqz10QLjESZvtLYo1dSmsCVOXejhM1SYDznSXoNgpS6EOfR5Dx53mN+JDiOxzvh2dmcpWGiMy1bUv8+6W/l2nfat3K/tl8sKDKmiDNNQPEzBpZ0woaa7pjYNZhcP1+cTZQXeBuVLNM0ydxFuA7KTeeuJzKTKlDlK3dEDpzoC8Jr0qLok3T3jtkP8hq34uxT5RTffC3PLZ5lLQvbP7X76+X0UfSf/R22WTSApIQp0Yh8n6rOLs3nUGPGpHFT/Vu3n9zKphpKE0KrG71LdGTAp1e/poopsknLnBfdHNJXPnGzf1a6ippXNytLiqkrxtchxmV3Mucfb3WmMel6dGH845B3vzpvpvTfeQrM7etBH6iH71P6iCVgibaqaH0Nx/a7/MdJXLhaxxpr+3lrOLM1Aeufd3Jpg1e//QvbeauvIZXdPOmAOtNHVEZ34Wvrpf/cSmVeY97d8xV/3sOOfKo89Tx0fTy36mdlR+r241zM6fdEZzIJLpL04bY7bQ7aKWVv9xKWzbQP53S7/attvezU7Mw5M/ad7EOKyZq35ynFPnqY3aRp2nDuEGV9t2yVRjtknn/RUquMEcb/dWYe/Znduoti0xO1G5ibZvWeZ9bmBebP+1m/K7vKZftGoWPuYbTiSsdsxJTWI1643DQn0ecdIzFvts5xW/UHn8d1Fmq9627dT3Mc9nbb+smeMFcInT5iW/pXIOfR6Bdd75ql9jLPbLP395CVzitMCv/gAPYCrgAUwFPICpgAcwFfAApgIewFTAA5gKeABTAQ9gKuABTAU8gKmAB1GmaikcEvVnLJV6rCI/7XJy869fC+V9y4X7qVb7kX8nA7gR99cpCf2VTaG2w6WWRp2C1iLl1uWK8kIlY52p9sH7c/fqTzxdoeDKalY5cXLqRvD+3Guqlr5qhUocje4ylag61T54f+4z1fa0M0lKmlyO5fGm2uWh9sH7c4+p1sLftm23z0mUpqmlKbdxdlF++ZSpwfbB+3OHqe6E2o5KuWnkI+fUUPvg/Yk2NeApMcjZmT82T/XbB+9PpKnOwq+VY1LbapkEzgScy+i9v9M+eH+iTLU9JSeeK85BrcsV5VqKYDsFktW/i7iT/+A+iH6HVAx1nvQd1dg+ZP1LwPf+gAcwFfAApgIewFTAA5gKeABTAQ9gKuABTAU8gKmABzAV8ACmAh7AVMADmAp4AFMBD2Aq4AFMBTyAqYAHMBXwAKYCHsBUwAOYCngAUwEPYCrgAUwFPICpgAcwFfAApgIewFTAA5gKeABTAQ/+B1ER5x+XgrNZAAAAAElFTkSuQmCC" alt="" />
功能实现了,但是感觉有点low,例如:生成的字符比较单一,只能生成字母。我在想能不能让6个字符中出现两个数字呢?好,开始搞吧。
方法二:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
li = []
for i in range(6):
if i == 2 or i == 4:
num = random.randrange(0,10)
li.append(str(num)) #将数字转换为字符串,因为.join()方法无法处理数字类型,只能出现字符类型的数据
else:
temp = random.randrange(65,91)
c = chr(temp)
li.append(c)
result = "".join(li)
print(result)
结果:
DM5T9B
方法二满足了需求,6个字符中包括了2个数字。但是 大家有没有发现两个数字的位置比较固定,这也不符合正常人的逻辑啊。要求随机的啊,随机,随机。 好,知道了要求数字和字母出现的次数和位置随机。开始搞吧。
方法三:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
li = []
for i in range(6):
r = random.randrange(0,5)
if r == 2 or r == 4:
num = random.randrange(0,10)
li.append(str(num))
else:
temp = random.randrange(65,91)
c = chr(temp)
li.append(c)
result = "".join(li)
print(result)
结果:
384M4J
方法三:方法二的优化
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
li = []
for i in range(6):
r = random.randrange(0,5)
if i == r:
num = random.randrange(0,10)
li.append(str(num))
else:
temp = random.randrange(65,91)
c = chr(temp)
li.append(c)
result = "".join(li)
print(result)
结果:
N7WIHL
好了,已经实现了需求。
总结:
(1)random 是随机生成数字。
(2)i = random.randrange(0,5) 表示生成0-4的随机数,记住不包括5哦。 取值范围为: 1=< i < 5
(3)"".join(li) 表示把列表li 的值生成一个字符串中间不用任何符号分隔,所以用了 ""
例如:
li = ['A','B','C','D','E']
result = "".join(li)
print(result)
结果:这里使用了 "".join()
ABCDE
我们用 “_” 下划线 来分隔。使用"_".join()
li = ['A','B','C','D','E']
result = "_".join(li)
print(result)
结果:
A_B_C_D_E
(4)join() 只能处理字符,不能处理数字,不信? 好,那么我们试试吧。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
li = [1,2,3,4,5]
result = "_".join(li)
print(result)
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuUAAADVCAIAAAB/vRktAAAgAElEQVR4nO2dW8/kSFrn41PsB+HKt0ggoRUICQGDdlmam7IEe0NftRiJ970BlWZqKzh0J9M9fWC6BoR6UiBgy6oWh56c2WGgKYSYGT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AAALYOegUAAAC2DnoFAAAAtg56BQAAALYOegUAAAC2DnoFAAAAtg56BQAAALYOegUAAAC2DnoFAAAAtg56BQAAALbOfwGvWtzErO6aBAAAAABJRU5ErkJggg==" alt="" />
所以我们代码中使用了:str()
num = random.randrange(0,10)
li.append(str(num)) #将数字转换为字符串,因为.join()方法无法处理数字类型,只能出现字符类型的数据
(5)chr 和 ord
chr 将数字转换为ASII码中对应的字母
ord 将字母转换为ASII码中对应的数字
r = chr(65)
print(r) n = ord("A")
print(n)
结果:
A
65
python-生成随机字符的更多相关文章
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- python生成随机日期字符串
python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...
- Python生成随机字符串
利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, cho ...
- python生成随机整数
python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...
- 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间
生成二维码 /// <summary>/// 生成二维码/// </summary>public static class QRcodeUtils{private static ...
- 使用Python生成ASCII字符画
使用Python生成ASCII字符画 在很多的网站主页中或者程序的注释中会有一些好看的字符注释画.显得很牛逼的样子 例如: 知乎 _____ _____ _____ _____ /\ \ /\ \ / ...
- Python生成随机数组的方法小结
Python生成随机数组的方法小结 本文实例讲述了Python生成随机数组的方法.分享给大家供大家参考,具体如下: 研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,今天把Pyt ...
- python 生成随机字符串
1.生成随机字符串 #数字+字母+符号 def getRandChar(n): l = [] #sample = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^ ...
- Java中生成随机字符的方法总结
package learnExercise; public class RandomCharacter { public static char getRandomCharacter(char ch1 ...
随机推荐
- Test failed.尝试加载Oracle客户端库时引发BadImageFormatException
CodeSmith6.5不像前几个版本,需要用户手动添加oracle驱动,内部已经集成了oracle的驱动. 网上遇到很多win7 64位机子使用CodeSmith连接oracle的时候出现错误如下:
- hdu1272并查集入门
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- c#串口编程时,忽略跨线程检查报错
1.直接在main_Form_Load的初始化中加 //忽略跨线程检查 // System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls ...
- 【好程序员笔记分享】——Cocoapods集成
-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ Xcode集成POD教程 准备工作: 首先我们要在我们的电脑中安装POD,进入命令行,输入如下指令 sudo ...
- 对Spring.Net+NHibenate+Asp.Net Mvc+Easyui框架的个人认识
对Spring.Net+NHibenate+Asp.Net Mvc+Easyui框架的个人认识 初次接触Spring.Net+NHibenate+Asp.Net Mvc+Easyui框架,查阅了相 ...
- JWeb备忘录
一.好记性不如赖笔头-- 工具类: JUnit4使用 MyEclipse快捷键 知识点: JAVA反射 JavaSe教程 Java5新特性 Java6新特性 Java7新特性 Java8新特 ...
- Java抽象类深入理解-----模板方法设计模式(Templete Method)
模板方法设计模式(Templete Method) 定义一个操作中的算法骨架,而将一些可变部分的实现延迟到子类中. 模板方法设计模式使得子类可以不改变一个算法的结构即可重新定义该算法某些特定的步骤. ...
- 如何使用NSFetchedResultsController-备
不知不觉我们已经来到了Core Data系列教程的最后一部分了,在这里我们要讨论如何使用NSFetchedResultsController来优化我们的应用,提高应用的运行速度,减少其内存占用. 你是 ...
- VS2010中<无法打开包括文件:“iostream.h”:>错误解决方法
C/C++ code? 1 2 #include <iostream.h> 改为: C/C++ code? 1 2 #include <iostream> using name ...
- 在Android模拟器中经常出现以下错误 timeout Launch canceled!
Failed to install MainActivity.apk on device 'emulator-5554': timeoutLaunch canceled! 解决方法: window-& ...