Python字符转换

yipeiwu_com5年前Python基础

如:

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


下面我们可以开始来设计我们的大小写转换的程序了:

#!/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') 
输出结果: 
abc我abc 
ABC我ABC

   

相关文章

python 多个参数不为空校验方法

在实际开发中经常需要对前端传递的多个参数进行不为空校验,可以使用python提供的all()函数 if not all([arg1, arg2, arg3]): # 当 arg1,...

Python使用Pickle库实现读写序列操作示例

本文实例讲述了Python使用Pickle库实现读写序列操作。分享给大家供大家参考,具体如下: 简介 pickle模块实现了用于对Python对象结构进行序列化和反序列化的二进制协议。“...

Python开发常用的一些开源Package分享

一般安装完Python后,我会先装一些常用的Package。做个笔记,记录下来,以备查询: Web FrameWorks Tornado,访问:http://www.tornadoweb...

Python闭包执行时值的传递方式实例分析

本文实例分析了Python闭包执行时值的传递方式。分享给大家供大家参考,具体如下: 代码中有问题和问题的解释。 #!/usr/bin/python #coding: utf-8 #...

Python传递参数的多种方式(小结)

一 位置传递 没什么好过多讲解. # 位置传递实例: def fun1(a,b,c): return a+b+c print(fun1(1,2,3)) 输出: 6...