内置函数值 -- 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 ...
随机推荐
- zabbix action理解
Maintenance status not in maintenance 谷歌翻译:维护状态不在维护中,中文意思就是监控的设备有problem,触发器报警了,然后执行action {TRIGGE ...
- Git版本管理的简介与安装[一]
标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 git简介 很多人都知道,Linus在1991年创建了开源的Linux,从此,Lin ...
- adb命令介绍与使用
DB的概念 adb的全称为Android Debug Bridge,是起到调试桥的作用.通过adb,我们可以在ecplise中方便的通过DDMS来调试Android程序,其实他就是一个debug工具. ...
- 07_Python变量内存地址、小数据池
一.变量在内存中的地址 变量:用来标识(identify)一块内存区域.为了方便表示内存,我们操作变量实质上是在操作变量指向的那块内存单元.编译器负责分配.我们可以使用Python内建函数id()来获 ...
- libGDX-wiki发布
为方便大家学习和访问,我将libgdx的wiki爬取到doku-wiki下,专门建立了以下地址.欢迎大家来共同完善. http://wiki.v5ent.com
- 在SecureCRT中无需输入密码登录Linux主机
服务器端操作系统:CentOs 5.8客户端:SecureCRT5.1.3第一步:密钥对的生成.在SecureCRT中建立一个新的连接.protocol选ssh2hostname输入要连接的主机IP. ...
- Timer类的schedule和scheduleAtFixedRate 简单应用
Timer类可以用作定时任务,主要的方法有schedule和scheduleAtFixedRate. schedule(TimerTask task, Date time) 安排在指定的时间执行指定的 ...
- Google chrome浏览器中通过扩展调用本地应用程序以及和程序相互通讯(C++)
最近项目用到浏览插件的开发,IE用到的是BHO,chrome打算做成扩展. 但是和ie有一点不同,chrome扩展是基于html+js+css开发的,那么就会有二个问题 1. 代码和算法等容易被别人复 ...
- chrome_options
用法 from selenium.webdriver.chrome.options import Options chorme_option=Options() chorme_option.add ...
- 初探solr搜索
solr是一个基于lucene的搜索引擎,lucene是一个全文检索引擎的架构.solr在此之上进行了封装完善,变成了一个很流行实用的搜索引擎,可以应对绝大部分的搜索需求.使用搜索引擎有以下几点好处: ...