Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python中chr函数

作者:风皇霸陵   发布日期:2025-07-17   浏览:63

在Python中,chr 函数用于将指定的 Unicode 码点(整数)转换为对应的字符。它接受一个介于 0 到 1,114,111 之间(包括 0 和 1,114,111)的整数,并返回该整数对应的 Unicode 字符。

以下是 chr 函数的基本用法:


python
chr(i)
i:一个介于 0 到 1,114,111 之间的整数,表示 Unicode 码点。
示例
python
# 将 Unicode 码点 72 转换为字符
char = chr(72)
print(char)  # 输出: H

# 将 Unicode 码点 97 转换为字符
char = chr(97)
print(char)  # 输出: a

# 将 Unicode 码点 8364 转换为字符(欧元符号)
char = chr(8364)
print(char)  # 输出: €
注意事项
如果传入的整数不在有效范围内(0 到 1,114,111),chr 函数将抛出一个 ValueError 异常。
python
try:
    char = chr(1114112)  # 超出范围
except ValueError as e:
    print(e)  # 输出: chr() arg not in range(0x110000)
chr 函数与 ord 函数是互逆的。ord 函数用于将字符转换为对应的 Unicode 码点。
python
char = 'H'
unicode_code_point = ord(char)
print(unicode_code_point)  # 输出: 72

# 将码点再转换回字符
original_char = chr(unicode_code_point)
print(original_char)  # 输出: H
chr 函数在处理文本数据时非常有用,尤其是在需要基于 Unicode 码点操作字符时。

上一篇:python 文件存在

下一篇:python print %

大家都在看

python时间格式

python开发windows应用程序

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

python的for i in range

npm config set python

python代码简单

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站