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

   

相关文章

Python3单行定义多个变量或赋值方法

Python3单行定义多个变量或赋值方法

你甚至可以在一行内将多个值赋值给多个变量 >>> a , b = 45, 54 >>> a 45 >>> b 54 这个技巧用...

python多进程共享变量

本文实例为大家分享了python多进程共享变量的相关代码,供大家参考,具体内容如下 from multiprocessing import Process, Manager impo...

Pyqt QImage 与 np array 转换方法

项目使用Pyqt作为UI框架,使用相机线程捕捉image,并在QGraphicsView中显示,遇到以下问题: 1、采集的数据为nparray数据,需转换为QImage 转换代码如下:...

解决Ubuntu pip 安装 mysql-python包出错的问题

问题描述如下,报没有找到mysql_config环境变量 $ pip install mysql-python Collecting MySQL-python==1.2.5 (f...

详解python运行三种方式

方式一 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码。 linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口...