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定时执行指定函数的方法。分享给大家供大家参考。具体实现方法如下: # time a function using time.time() and the a...

如何在mac环境中用python处理protobuf

这篇文章主要介绍了如何在mac环境中用python处理protobuf,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 br...

在Pycharm terminal中字体大小设置的方法

如下所示: file->settings->Editor->General->Console里面的console commands history size 以上...

python对视频画框标记后保存的方法

需要画框取消注释rectangle import cv2 import os,sys,shutil import numpy as np # Open the input mov...

python列表,字典,元组简单用法示例

本文实例讲述了python列表,字典,元组简单用法。分享给大家供大家参考,具体如下: 列表 #_*_ coding:utf-8 _*_ # 列表,定义在方括号的形式中,可以进行切片操...