内置函数值 -- 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', 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
内置函数值 -- chr() ord() -- 字符和ascii的转换的更多相关文章
- python内置函数值 -- chr() ord()
chr()接收一个数字, 找到这个数字对应的ascii里的元素(只能接受数字) a = chr(65) print(a) #结果: A ord()接收一个字符,返回这个字符对应的数字.(只能接受一个字 ...
- Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...
- python中字符与ascii码转换
ASCII码转字符用chr()函数: 字符转ASCII码用ord()函数:
- Python字符与ASCII码转换
有两个内置函数,记得以前在<Python Cookbook>里看到过. >>>print ord('a') 97 >>>print chr(97) a
- js 字符与ascii码转换
参考 http://www.jb51.net/article/43534.htm ' '.charCodeAt(); //字符转ascii String.fromCharCode(10); //a ...
- Python内置函数(16)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- Python内置函数(48)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- Python标准库:内置函数chr(i)
返回一个參数i表示的字符串. 比方,chr(97)返回字符"a".參数i的有效范围为0到1.114,111(0x10FFFF),其他范围的值会抛出异常ValueError. 与之相 ...
- js字母/字符与ASCII码转换
var tempStr="A"; console.log(tempStr.charCodeAt());// 65 ,转ASCII码 console.log(String.fromC ...
随机推荐
- base64_encode与base64_decode
base64_encode 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! base64_encode() returns 使用 base64 对 data 进行编码. ...
- JAVA中发送电子邮件的方法
JAVA中发送邮件的方法不复杂,使用sun的JavaMail的架包就可以实现. 一.下载JavaMail的架包,并导入项目中,如下: 二.附上代码例子,如下: 1.在main函数中对各项参数进行赋值 ...
- test for python urllib
#!/usr/bin/python import urllib2 import time import logging import threading succCount = 0 failCount ...
- 《.NET 设计规范》第 7 章:异常
第 7 章:异常 异常与各种面向对象语言集成得非常好. 异常增强了 API 的一致性. 在用返回值来报告错误时,错误处理的代码与可能会发生错误的代码距离总是很近. 更容易使错误处理的带码全局化. 错误 ...
- Cypher查询语言--Neo4j 入门 (一)
目录 操作符 参数 标识符 注解 Start 通过id绑定点 通过id绑定关系 通过id绑定多个节点 所有节点 通过索引查询获取节点 通过索引查询获取关系 多个开始点 Cypher是一个描述性的图形 ...
- python正则实现简单计算器
利用正则实现计算器 利用正则来实现简单计算器的功能,能够设计计算带括号的加减乘除运算.当然不使用eval等语句. 利用递归: import re from functools import reduc ...
- Golang时间格式化
PHP中格式化时间很方便,只需要一个函数就搞定: date("Y-m-d H:i:s") 而在Golang中,用的是"2006-01-02 15:04:05"这 ...
- spring boot + vue + element-ui全栈开发入门——前端列表页面开发
一.页面 1.布局 假设,我们要开发一个会员列表的页面. 首先,添加vue页面文件“src\pages\Member.vue” 参照文档http://element.eleme.io/#/zh-CN ...
- BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]
2287. [HZOI 2015]疯狂的机器人 题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数 这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg 其实就是 ...
- CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
Axel and Marston in Bitland 好开心第一次补$F$题虽然是$Div.2$ 题意: 一个有向图,每条边是$0$或$1$,要求按如下规则构造一个序列然后走: 第一个是$0$,每次 ...