[CookBook_V2 笔记]字符和字符值之间的转换[1-2]
来源:互联网 发布:php 去除数组重复元素 编辑:程序博客网 时间:2024/06/10 04:38
概述:将一个字符转化为相应死亡ascii码或者反其道而行之.
首先是书上的 ord 和 chr 函数
>>> ord('A')65>>> chr(66)'B'
我们先看看ord函数
ord(...) ord(c) -> integer Return the integer ordinal of a one-character string.
意思是接受一个整型数据,并且返回这个整型数据所对应的单个字符
然后是 chr 函数
chr(...) chr(i) -> character Return a string of one character with ordinal i; 0 <= i < 256.
意思是接受一个字符型数据(c中的char型),返回对应的字符值,该字符值在0-255之间。
以上都是针对ascii字符的,那么unicode的在下面
下面介绍unichr函数
unichr(...) unichr(i) -> Unicode character Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.意思是接受一个unicode字符,返回其对应的字符值,和chr 函数的功能基本相同
而ord 函数也可以处理unicode 字符
>>> ord(u'\u2020')8224>>> repr(unichr(8224))"u'\\u2020'">>> unichr(8224)u'\u2020'
关于python中的unicode的事情打算在下次写出.
然后关于 str函数:
可能有人会混淆chr 和 str函数:
class str(basestring) | str(object='') -> string | | Return a nice string representation of the object. | If the argument is a string, the return value is the same object. | | Method resolution order: | str | basestring | object | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y | | __contains__(...) | x.__contains__(y) <==> y in x | | __eq__(...) | x.__eq__(y) <==> x==y
其实str函数是将输入的任意整数转化成字符串
而chr是将输入的数字转化为字符输出
也就是说
>>> str(2345)'2345'>>> str(97)'97'>>> chr(97)'a'
0 0
- [CookBook_V2 笔记]字符和字符值之间的转换[1-2]
- 字符和字符值之间的转换
- Python - 字符和字符值之间的转换
- python字符和字符值之间的转换
- python学习笔记-半角字符和全角之间的转换
- 字符之间的转换
- 字符之间的转换
- 整形和字符之间的转换
- 字符串和字符数组之间的转换
- 宽字符和UTF8之间的转换
- COLORREF 和字符之间的转换
- 字符串和字符数组之间的转换
- 字符和整数之间的转换
- 字符串和字符数组之间的转换
- 字符串和字符数组之间的转换
- 【oracle】 日期和字符之间的转换
- 字符串和字符数组之间的转换
- 全角字符和半角字符之间的转换
- 【概率DP】 ZOJ 2949 Coins of Luck 期望
- 第10题:翻转句子中单词的顺序
- Google推荐的图片加载库Glide介绍
- [HNOI2004]打鼹鼠 解题报告
- 转:could not find the main class, Program will exit (说明编译器compiler版本要不高于installed JREs,JVM版本)
- [CookBook_V2 笔记]字符和字符值之间的转换[1-2]
- Leetcode: Word Break
- 德莱联盟
- WebService介绍
- C++中struct和class的区别及C++ this变量和const变量类型
- 最长公共子序列求解:递归与动态规划方法
- Max Points on a Line
- 《Linux/UNIX系统编程手册》 英文版读书笔记 Alternative I/O Models63.2
- VS2010 Qt代码提示