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提供多种数据类型来存放数据项集合,主要包括序列(列表list和元组tuple),映射(如字典dict),集合(set),下面对这几种一一介绍: 一 序列 1.列表list 列...

Python新手在作用域方面经常容易碰到的问题

通常,当我们定义了一个全局变量(好吧,我这样说是因为讲解的需要——全局变量是不好的),我们用一个函数访问它们是能被Python理解的:   bar = 42 def fo...

python3.6 tkinter实现屏保小程序

本文实例为大家分享了python3.6 tkinter实现屏保小程序,供大家参考,具体内容如下 该小程序是在闲着没事的时候,随便写的,就当打发无聊了。 该程序是用python3.6写的,...

Python3实现将一维数组按标准长度分隔为二维数组

如下所示: def trans_data_to_pair(self,data,index): contents=[ data[i:i+index] f...

Python3+Appium安装使用教程

Python3+Appium安装使用教程

一、安装 我们知道selenium是桌面浏览器自动化操作工具(Web Browser Automation) appium是继承selenium自动化思想旨在使手机app操作也能自动化的...