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中类的一些方法,分享给大家供大家参考。具体分析如下: 先来看看下面这段代码: class Super: def delegate(self):...

详解DeBug Python神级工具PySnooper

PySnooper 在 GitHub 上自嘲是一个“乞丐版”调试工具(poor man's debugger)。 一般情况下,在编写 Python 代码时,如果想弄清楚为什么 Pytho...

python sklearn库实现简单逻辑回归的实例代码

Sklearn简介 Scikit-learn(sklearn)是机器学习中常用的第三方模块,对常用的机器学习方法进行了封装,包括回归(Regression)、降维(Dimensional...

Python中函数参数调用方式分析

本文实例讲述了Python中函数参数调用方式。分享给大家供大家参考,具体如下: Python中函数的参数是很灵活的,下面分四种情况进行说明。 (1) fun(arg1, arg2, .....

python利用elaphe制作二维条形码实现代码

手机上的二维码识别程序已经做的很好了,“我查查”用起来很不错的 我搜集了几个二维条码生成网站: http://www.morovia.com/free-online-barcode-ge...