内置函数值 -- chr() ord() -- 字符和ascii的转换

简介: chr(i)  Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string 'a', while chr(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). Valu

英文文档:


chr(i) 

 Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string 'a', while chr(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. 函数返回整形参数值所对应的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

目录
打赏
0
0
0
0
19
分享
相关文章
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
2267 0
|
5月前
|
[oeasy]python035_根据序号得到字符_chr函数_字符_character_
本文介绍了Python中的`ord()`和`chr()`函数。`ord()`函数通过字符找到对应的序号,而`chr()`函数则根据序号找到对应的字符。两者互为逆运算,可以相互转换。文章还探讨了单双引号在字符串中的作用,并解释了中文字符和emoji也有对应的序号。最后总结了`ord()`和`chr()`函数的特点,并提供了学习资源链接。
42 4
[oeasy]python034_计算机是如何认识abc的_ord函数_字符序号_ordinal_
[oeasy]python034_计算机是如何认识abc的_ord函数_字符序号_ord
42 0
charAt()方法- 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1,开头的为0, 用来获取单个字符的
charAt()方法- 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1,开头的为0, 用来获取单个字符的
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
split(分割符)字符串转换为数组,str_arr.split(‘‘)//以空字符串为分隔符,以,为分隔符str_arr.split(‘,‘)str.replace(‘1‘,‘a‘)替换1
split(分割符)字符串转换为数组,str_arr.split(‘‘)//以空字符串为分隔符,以,为分隔符str_arr.split(‘,‘)str.replace(‘1‘,‘a‘)替换1
语音识别,运算符,字符串的三种成定义方式,\“转意字符的定义,字符串的拼接,TypeError: can only concatenate str (not “init “) to str是浮点数和整
语音识别,运算符,字符串的三种成定义方式,\“转意字符的定义,字符串的拼接,TypeError: can only concatenate str (not “init “) to str是浮点数和整
luatos 字符串与数组转换,解析hex数组
luatos 字符串与数组转换,解析hex数组
140 1
[oeasy]python0112_扩展ascii_Extended_ascii_法文字符
[oeasy]python0112_扩展ascii_Extended_ascii_法文字符
127 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等