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

   

相关文章

anaconda中更改python版本的方法步骤

anaconda中更改python版本的方法步骤

anaconda是一个非常好用的python发行版本,其中包含了大部分常用的库。 最新的anaconda中python版本已经更新到了python3.6,而tensorflow只支持py...

Python从文件中读取数据的方法讲解

编写了一个名为learning_python.txt的文件,内容如下: [root@centos7 tmp]# cat learning_python.txt In Python...

命令行运行Python脚本时传入参数的三种方式详解

如果在运行python脚本时需要传入一些参数,例如gpus与batch_size,可以使用如下三种方式。 python script.py 0,1,2 10 python scrip...

对Python3中dict.keys()转换成list类型的方法详解

对Python3中dict.keys()转换成list类型的方法详解

在python3中使用dict.keys()返回的不在是list类型了,也不支持索引,我们可以看一下下面这张图片 那么我们应该怎么办呢,其实解决的方法也是非常简单的,只需要使用list...

pycharm+PyQt5+python最新开发环境配置(踩坑)

pycharm+PyQt5+python最新开发环境配置(踩坑)

安装工具: Pycharm 专业版2017.3 PyQT5 python3 测试工程文件及所需工具: 1、首先安装Pycharm,先前一篇介绍安装破解版的可以参...