简介:字符 - ASCII 码相互转换

1、chr() 用一个整数作参数,返回一个对应的字符。

2、ord() 函数是 chr() 函数(对于 8 位的 ASCII 字符串)的配对函数,它以一个字符串(Unicode 字符)作为参数,返回对应的 ASCII 数值,或者 Unicode 数值。

案例源码:

# -*- coding: utf-8 -*-
# time: 2022/5/21 11:11
# file: String2Ascii.py
# 公众号: 玩转测试开发

# 字符转Ascii
# ord => Return the Unicode code point for a one-character string.
print(ord("a"))  # 97
print(ord("发"))  # 21457

# Ascii转字符
# chr => Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
print(chr(97))  # a
print(chr(36130))  # 财

标准ASCII一览表:

微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!

更多推荐

Python:字符 - ASCII 码相互转换