英文文檔:
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 of ord().
The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16). ValueError will be raised if i is outside that range
說明:
1.函數(shù)返回整形參數(shù)值所對(duì)應(yīng)的Unicode字符的字符串表示
>>> chr(97) #參數(shù)類型為整數(shù)
'a'
>>> chr('97') #參數(shù)傳入字符串時(shí)報(bào)錯(cuò)
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函數(shù)剛好相反
>>> chr(97)
'a'
>>> ord('a')
97
3.傳入的參數(shù)值范圍必須在0-1114111(十六進(jìn)制為0x10FFFF)之間消请,否則將報(bào)ValueError錯(cuò)誤
>>> chr(-1) #小于0報(bào)錯(cuò)
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報(bào)錯(cuò)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
chr(1114112)
ValueError: chr() arg not in range(0x110000)