目錄
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
微信公衆號 掃一掃關注