python chr/ord函數區別和使用

目錄

一.chr函數將ascll碼轉爲字符

二.ord函數將字符轉爲ascll碼

三.chr和ord配合使用生成隨機字符串(隨機驗證碼)

 

 


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教程公衆號

                                                                                            猿說python

                                                                                 微信公衆號 掃一掃關注

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章