python 中的 用chr()数值转化为字符串,字符转化为数值ord(s)函数
1.1 python字符串定义
-
#!/usr/bin/python
-
# -*- coding: utf8 -*-
-
# 定义一个字符串
-
s1 = 'this is long String that spans two lines'
-
# 表示下面一行是上一行的延续
-
s2 = 'this is long String\
-
that spans two lines'
-
#原样输出字符串
-
s3 = """this is long String
-
that spans two lines
-
"""
-
# 换行输出字符串
-
s4 = 'this is long String\n\
-
that spans two lines'
-
print "s1=",s1
-
print "s2=",s2
-
print "s3=",s3
-
print "s4=",s4
输出结果:
1.2 python字符与字符值之间的转换
A :字符转化为数值&数值转化为字符串:其中ord函数可以把字符串转化为整数的范围为【0,65535】
-
#!/usr/bin/python
-
# -*- coding: utf8 -*-
-
# 字符转化为数值
-
s = 'a'
-
print ord(s)
-
# 数值转化为字符串
-
b = 97
-
print chr(b)
B:chr()函数的参数args在【0,256】,意思就是只能把【0,256】的数字转化为字符串
C:把一个Unicode码值转化为一个Unicode字符串
-
#!/usr/bin/python
-
# -*- coding: utf8 -*-
-
#把unicode码值转化为字符串
-
print repr(unichr(8224))
-
#把unicode字符串转化为码值
-
print repr(ord(u'\u2020'))
D:chr()函数与str函数的区别:
1. chr()函数是把一个小整数作为参数,并返回对应于ASCII单字符的字符串
2. str()函数是把任何整数作为参数,返回一个该整数的文本形式的字符串
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· DeepSeek “源神”启动!「GitHub 热点速览」
· 上周热点回顾(2.17-2.23)