python中 内置函数 chr 和 内置函数 ord 可以配对使用;chr函数将ascll码转为字符;ord函数将字符转为ascll码;
一.chr函数将ascll码转为字符
chr(65) >>>> "A"
二.ord函数将字符转为ascll码
ord("A")
>>>> 65
三.chr和ord配合使用生成随机字符串(随机验证码)
import random
for i in range(10):
str1=''
for i in range(8):
ch=chr(random.randrange(ord('a'),ord('z')))
str1+=ch
print(str1)
输出:
jrcrvwpr diqufjkx mgmvsaaa ajswbgse xwokswsw xttbhygs ehubdtje nppqyisd qxrjopun ubcwepwi
猜你喜欢:
4.python bytearray/bytes/string区别
转载请注明:猿说Python » python chr/ord函数区别和使用
技术交流、商务合作请直接联系博主
扫码或搜索:猿说python

猿说python
微信公众号 扫一扫关注
来源:https://www.cnblogs.com/shuopython/p/12502089.html