Home  >  Article  >  Backend Development  >  Python字符转换

Python字符转换

WBOY
WBOYOriginal
2016-06-16 08:47:381317browse

如:
>>> 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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn