Python ord() and chr() are built-in functions. They are used to convert a character to an int and vice versa.

Python ord()和chr()是内置函数。 它们用于将字符转换为int,反之亦然。

Python ord() and chr() functions are exactly opposite of each other.

Python的ord()和chr()函数彼此完全相反。

Python ord() (Python ord())

Python ord() function takes string argument of a single Unicode character and return its integer Unicode code point value. Let’s look at some examples of using ord() function.

Python ord()函数采用单个Unicode字符的字符串参数,并返回其整数Unicode代码点值。 让我们看一些使用ord()函数的示例。

x = ord('A')
print(x)print(ord('ć'))
print(ord('ç'))
print(ord('$'))

Output:

输出:

65
263
231
36

Python的chr() (Python chr())

Python chr() function takes integer argument and return the string representing a character at that code point.

Python chr()函数采用整数参数,并返回表示该代码点处字符的字符串 。

y = chr(65)
print(y)
print(chr(123))
print(chr(36))

Output:

输出:

A
{
$
ć

Since chr() function takes an integer argument and converts it to character, there is a valid range for the input.

由于chr()函数采用整数参数并将其转换为字符,因此输入存在有效范围。

The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in hexadecimal format). ValueError will be raised if the input integer is outside that range.

参数的有效范围是从0到1,114,111(十六进制格式为0x10FFFF)。 如果输入整数超出该范围,将引发ValueError。

chr(-10)

Output:

输出:

ValueError: chr() arg not in range(0x110000)

Let’s see an example of using ord() and chr() function together to confirm that they are exactly opposite of another one.

让我们来看一个使用ord()和chr()函数来确认它们与另一个完全相反的示例。

print(chr(ord('ć')))
print(ord(chr(65)))

Output:

输出:

ć
65

That’s all for a quick introduction of python ord() and chr() functions.

这就是快速介绍python ord()和chr()函数的全部内容。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation – ord, Official Documentation – chr

参考: 官方文档– ord , 官方文档– chr

翻译自: https://www.journaldev.com/22767/python-ord-chr

Python ord(),chr()函数相关推荐

  1. python中chr函数的用法_python中hex,oct,chr,ord函数讲解

    hex() 中文说明: 转换一个整数对象为十六进制的字符串表示 英文说明 hex(...) hex(number) -> string Return the hexadecimal repres ...

  2. python中chr函数的用法_【转】Python内置函数(10)——chr

    英文文档: chr(i) Return the string representing a character whose Unicode code point is the integeri. Fo ...

  3. python中chr函数的作用_chr()函数以及Python中的示例

    Python chr() 功能 chr()function是Python中的一个库函数,用于从给定的ASCII码(整数值)中获取字符值,它接受一个数字(应为ASCII码)并返回字符. 语法: chr( ...

  4. python中的ord,chr函数

    chr().unichr()和ord() chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符.unichr()跟它一样,只不过返回的是Unicode字符 ...

  5. [转载] Python: ord()函数

    参考链接: Python ord() ch() , unichr() , ord() ①chr()函数用一个范围在range(256)内的整数作参数,返回一个对应的字符. >>>ch ...

  6. Python: ord()函数

    ch() , unichr() , ord() ①chr()函数用一个范围在range(256)内的整数作参数,返回一个对应的字符. >>>chr(65) 'A' ②unichr() ...

  7. python中chr的用法_python中chr()函数和ord()函数的用法

    原博文 2018-06-11 21:52 − 一,chr()函数 格式:Chr(<数值表达式>) 说明:函数返回值类型为String,其数值表达式值取值范围为0~255. 例如:Print ...

  8. python语句中ord_浅谈Python中chr、unichr、ord字符函数之间的对比

    ord是unicode ordinal的缩写,即编号 chr是character的缩写,即字符 ord和chr是互相对应转换的. 但是由于chr局限于ascii,长度只有256,于是又多了个unich ...

  9. Python ord()函数和chr()函数

    ord()函数 描述: 以一个字符串(Unicode 字符)作为参数,返回对应的 ASCII 数值,或者 Unicode 数值.例如,ord('a')返回整数97,ord('€')(欧元符号)返回83 ...

  10. 树莓派i2c python_树莓派2 python i2cPython中chr、unichr、ord字符函数之间的对比

    chr.unichr.ord在Python中都可以被用作字符类型转换,这里我们就来浅谈Python中chr.unichr.ord字符函数之间的对比,需要的朋友可以参考下ord是unicode ordi ...

最新文章

  1. ESP32cam蓝牙模块与arduino uno通信实验
  2. Array.prototype.slice.call(arguments)对象转成数组
  3. python函数的目的与意义_Python函数的概念和使用
  4. imgkit分辨率_pythonhtml2image: imgkit 和 wkhtmltoimage的坑
  5. 哪吒U Pro试驾:透明A柱超实用、满电500公里保底续航
  6. java基础学习之对象转型
  7. 【java】对学生成绩进行排序
  8. An Implemention of Realtime Gobal Illumination
  9. HTML跳转php没反应的问题解决
  10. odb访问mysql数据库(odb的简单用法1)
  11. 6U VPX 高性能数据存储板 (2 片XC7K325T)
  12. linux找回cp之前的文件,Linux中找回误删除的文件
  13. linux操作系统没声音,Linux系统下没有声音的解决方案
  14. 【黑马旅游网】项目完结+未完成功能实现+个人总结+bug记录
  15. wildcard 的理解
  16. java培训包装简历模板
  17. 如何获取淘宝/天猫店铺的所有商品 API数据
  18. 黑莓招聘 BlackBerry Developer Evangelist (based in China)
  19. Think Pad笔记本分区解决思路及方法
  20. 看历史涨知识!高速公路知多少?

热门文章

  1. angular中的MVVM模式
  2. 通用的linux下安装配置svn独立服务
  3. Asynchronous HTTP Requests in Android Using Volley
  4. [转载] Java中final关键字
  5. vss中项目与服务器断开绑定之后进行重新绑定得方法
  6. CF EDU - E. Lomsat gelral 树上启发式合并
  7. 当网卡收到的包的目的地址是主机上另一个网卡的地址.arp总结
  8. cf519D . A and B and Interesting Substrings 数据结构map
  9. linq里的select和selectmany操作 投影运算
  10. 再解Java中的String