Python字符与ASCII码之间的转换

来源:互联网 发布:仙桃数据谷最新招聘 编辑:程序博客网 时间:2024/05/05 19:04

Python提供了ord和chr两个内置的函数,用于字符与ASCII码之间的转换。

如:
>>> print ord('a') 
97 
>>> print chr(97) 

下面我们可以开始来设计我们的大小写转换的程序了: 
<span style="font-family:Microsoft YaHei;font-size:12px;">#!/usr/bin/env python #coding=utf-8 def UCaseChar(ch): if ord(ch) in range(97, 122): return chr(ord(ch) - 32) return ch def LCaseChar(ch): if ord(ch) in range(65, 91): return chr(ord(ch) + 32) return ch def UCase(str): return ''.join(map(UCaseChar, str)) def LCase(str): return ''.join(map(LCaseChar, str)) print LCase('ABC我abc') print UCase('ABC我abc') </span>
输出结果: 
abc我abc 
ABC我ABC

转载自:

http://www.360doc.com/content/12/1115/19/54470_248056514.shtml

0 0