Python字符转换

yipeiwu_com6年前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实现小世界网络生成

没有使用igraph库哦 因为我还没学 小世界网络简介: 1998年, Watts和Strogatz 提出了小世界网络这一概念,并建立了WS模型。实证结果表明,大多数的真实网络都具有小世...

Python中的hypot()方法使用简介

 hypot()方法返回的欧几里德范数 sqrt(x*x + y*y). 语法 以下是hypot()方法的语法: hypot(x, y) 注意:此函数是无法直接访问的...

python3.0 字典key排序

IDLE 3.0 >>> dic = {"aa":1,"bb":2,"ab":3} >>> dic {'aa': 1, 'ab': 3, 'bb':...

Python3中使用PyMongo的方法详解

前言 本文主要给大家介绍的是关于在Python3使用PyMongo的方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细介绍: MongoDB存储 在这里我们来看一下Python...

Python实现的排列组合、破解密码算法示例

本文实例讲述了Python实现的排列组合、破解密码算法。分享给大家供大家参考,具体如下: 排列组合(破解密码) 1.排列 itertools.permutations(iterabl...