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中TCP协议中的粘包问题

TCP协议中的粘包问题 1.粘包现象 基于TCP实现一个简易远程cmd功能 #服务端 import socket import subprocess sever = socket.s...

Python守护进程用法实例分析

本文实例讲述了Python守护进程用法。分享给大家供大家参考。具体分析如下: 守护进程是可以一直运行而不阻塞主程序退出。要标志一个守护进程,可以将Process实例的daemon属性设置...

PyQt5 pyqt多线程操作入门

首先来看一个例子: # coding=utf-8 __author__ = 'a359680405' from PyQt5.QtCore import * from PyQ...

Python中@property的理解和使用示例

本文实例讲述了Python中@property的理解和使用。分享给大家供大家参考,具体如下: 重看狗书,看到对User表定义的时候有下面两行 @property def pa...

pytorch实现onehot编码转为普通label标签

label转onehot的很多,但是onehot转label的有点难找,所以就只能自己实现以下,用的topk函数,不知道有没有更好的实现 one_hot = torch.tensor...