内置函数值 -- 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).
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
内置函数值 -- 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 ...
随机推荐
- Redis清空数据
进入redis目录下 redis-cli -h IP -p 端口 -a 密码 flushall
- 机器学习-GBDT和XGboost
参考: 陈天奇slides : https://homes.cs.washington.edu/~tqchen/pdf/BoostedTree.pdf Friedman GBDT 论文: htt ...
- Android ButterKnife注解式开发
在Android开发中findViewById和setOnClickListener解脱写法. 在任意的一个类中 @Bind(R.id.et) EditText editText; @OnClick( ...
- flask----flask-session
一.flask-session flask-session是flask框架的session组件,由于原来flask内置session使用签名cookie保存,该组件则将支持session保存到多个地方 ...
- exp/imp 多用户导入导出
创建用户 创建三个用户test1,test2,test3及表table1,table2,table3 SQL> create user test1 identified by test1 def ...
- mvn 手动安装jar 到本地库
安装: mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.1.0.7.0 -Dpackagi ...
- Effective Java 之-----返回零长度的数组或集合而不是null
如下代码,通常用户列表为空时,会习惯性返回null,因为这时会认为:null返回值比零长度数组更好,因为它避免了分配数组所需要的开销. private final List<UserBean&g ...
- 2018Pycharm激活方法
1.将"0.0.0.0 account.jetbrains.com"添加到hosts文件中 2.打开http://idea.lanyus.com/ 3.获取激活码,粘贴到第二个选项 ...
- 图说:为什么Java中的字符串被定义为不可变的
8张图,看懂Java字符串的不变性 字符串,想必大家最熟悉不过了,通常我们在代码中有几种方式可以创建字符串,比如:String s = "Hollis";这时,其实会在堆内存中创建 ...
- wxPython实现在浏览器中打开链接
需要用到webbrowser模块 代码超简单: import webbrowserwebbrowser.open('http://www.wangxing.com') webbrowser.open( ...