Python字符轉(zhuǎn)換
更新時間:2008年09月06日 13:21:35 作者:
Python提供了ord和chr兩個內(nèi)置的函數(shù),用于字符與ASCII碼之間的轉(zhuǎn)換。
如:
>>> print ord('a')
97
>>> print chr(97)
a
下面我們可以開始來設(shè)計我們的大小寫轉(zhuǎn)換的程序了:
#!/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')
輸出結(jié)果:
abc我abc
ABC我ABC
>>> print ord('a')
97
>>> print chr(97)
a
下面我們可以開始來設(shè)計我們的大小寫轉(zhuǎn)換的程序了:
復(fù)制代碼 代碼如下:
#!/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
相關(guān)文章
python中l(wèi)eastsq函數(shù)的使用方法
這篇文章主要介紹了python中l(wèi)eastsq函數(shù)的使用方法,leastsq作用是最小化一組方程的平方和,下面文章舉例說明詳細內(nèi)容,具有一的參考價值,需要的小伙伴可以參考一下2022-03-03Python中g(shù)etattr函數(shù)和hasattr函數(shù)作用詳解
這篇文章主要介紹了Python中g(shù)etattr函數(shù)和hasattr函數(shù)作用的相關(guān)知識,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06淺談cv2.imread()和keras.preprocessing中的image.load_img()區(qū)別
這篇文章主要介紹了淺談cv2.imread()和keras.preprocessing中的image.load_img()區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06通過python讀取txt文件和繪制柱形圖的實現(xiàn)代碼
這篇文章主要介紹了通過python讀取txt文件和繪制柱形圖的實現(xiàn)代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Pytorch關(guān)于Dataset?的數(shù)據(jù)處理
這篇文章主要介紹了Pytorch關(guān)于Dataset?的數(shù)據(jù)處理,學習如何對卷積神經(jīng)網(wǎng)絡(luò)編程;首先,需要了解Pytorch對數(shù)據(jù)的使用,也是在我們模型流程中對數(shù)據(jù)的預(yù)處理部分,下面我們就一起進入文章查看具體處理過程吧2021-12-12