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
猜你喜歡:
1.python bytes函式
2.python bytearray函式
3.python bytes和string相互轉換
4.python bytearray/bytes/string區別
轉載請注明:猿說Python ? python chr/ord函式區別和使用
技術交流、商務合作請直接聯系博主 掃碼或搜索:猿說python
猿說python
微信公眾號 掃一掃關注
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/176256.html
標籤:Python
上一篇:Python3標準庫:tempfile臨時檔案系統物件
下一篇:實體003:完全平方數
