英文文檔:
-
chr(i) -
Return the string representing a character whose Unicode code point is the integer i. For example,
chr(97)returns the string'a', whilechr(8364)returns the string'€'. This is the inverse oford(). -
The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16).
ValueErrorwill be raised if i is outside that range - 說明:
- 1. 函數返回整形參數值所對應的Unicode字符的字符串表示
>>> chr(97) #參數類型為整數
'a'
>>> chr('97') #參數傳入字符串時報錯
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
chr('97')
TypeError: an integer is required (got type str)
>>> type(chr(97)) #返回類型為字符串
<class 'str'>
2. 它的功能與ord函數剛好相反
>>> chr(97)
'a'
>>> ord('a')
97
3. 傳入的參數值范圍必須在0-1114111(十六進制為0x10FFFF)之間,否則將報ValueError錯誤
>>> chr(-1) #小於0報錯
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
chr(-1)
ValueError: chr() arg not in range(0x110000)
>>> chr(1114111)
'\U0010ffff'
>>> chr(1114112) #超過1114111報錯
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
chr(1114112)
ValueError: chr() arg not in range(0x110000)
簡單描述
chr接收一個數字, 找到這個數字對應的ascii里的元素(只能接受數字)
a = chr(65)
print(a) #結果: A
ord()接收一個字符,返回這個字符對應的數字.(只能接受一個字符)
b = ord('a')
print(b) #結果: 97

